2011-10-24 36 views
1

這裏是我的SQL數據源的詳細信息SqlDataSource的選擇參數

<asp:SqlDataSource ID="dsMoodleQuiz" runat="server" 
    ConnectionString="<%$ ConnectionStrings:OnlineMeetingConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:OnlineMeetingConnectionString.ProviderName %>" 

    SelectCommand="SELECT Name, UserID, Grade, FirstName, LastName, Email, TimeModified, IDNumber FROM tbMoodleQuiz WHERE (FirstName = @FirstName) AND (LastName = @LastName)" 
    onselecting="dsMoodleQuiz_Selecting"> 
    <SelectParameters> 
     <asp:Parameter Name="FirstName" /> 
     <asp:Parameter Name="LastName" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

一個GridView的名字gvDetails連接到dsMoodleQuiz。在button click我想要

gridview得到填充。

這裏是按鈕的代碼單擊

protected void btnSearch_Click(object sender, EventArgs e) 
{ 
    dsMoodleQuiz.SelectParameters.Add("@FirstName", System.Data.DbType.String, "Jhon"); 
    dsMoodleQuiz.SelectParameters.Add("@LastName", System.Data.DbType.String, "Wald"); 

    GridView1.DataBind(); 
} 

這是爲什麼不工作...?我是否缺少任何代碼...?感謝幫助

回答

1

這是一個關於如何搜索網格視圖和填充效果使用的SqlDataSource是搜索標準樣品例如.....

網格視圖結合.....

 <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" AllowPaging="True" 
AllowSorting="true" DataSourceID="dsGridview" Width="540px" PageSize="10"> 
<Columns> 
    <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" /> 
    <asp:TemplateField HeaderText="First Name" SortExpression="FirstName"> 
     <ItemStyle Width="120px" HorizontalAlign="Left" /> 
     <ItemTemplate> 
      <asp:Label ID="lblFirstname" Text='<%# HighlightText(Eval("FirstName")) %>' 
       runat="server" /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Last Name" SortExpression="LastName"> 
     <ItemStyle Width="120px" HorizontalAlign="Left" /> 
     <ItemTemplate> 
      <asp:Label ID="lblLastname" Text='<%# HighlightText(Eval("LastName")) %>' 
      runat="server" /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:BoundField DataField="Department" HeaderText="Department" 
     SortExpression="Department" ItemStyle-Width="130px" /> 
    <asp:BoundField DataField="Location" HeaderText="Location" 
     SortExpression="Location" ItemStyle-Width="130px" /> 
</Columns> 
</asp:GridView> 

和SQL數據源是這樣的...

<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT * FROM People" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    FilterExpression="firstname like '%{0}%' or lastname like '%{1}%'"> 
    <FilterParameters> 
     <asp:ControlParameter Name="firstname" ControlID="txtSearch" PropertyName="Text" /> 
     <asp:ControlParameter Name="lastname" ControlID="txtSearch" PropertyName="Text" /> 
    </FilterParameters> 
    </asp:SqlDataSource> 

添加文本框進行搜索..

<asp:TextBox ID="txtSearch" runat="server" /> 
<asp:ImageButton ID="btnSearch" ImageUrl="images/searchbutton.png" runat="server" /> 
<asp:ImageButton ID="btnClear" ImageUrl="images/clearbutton.png" runat="server" /> 

,這是用於結合和將文本輸入到文本框中

using System.Text.RegularExpressions; 
Partial; 
class GridviewwithHighlightedSearch : System.Web.UI.Page { 

    // Create a String to store our search results 
    private string SearchString = ""; 

    string HighlightText(string InputTxt) { 
     // This function is called whenever text is displayed in the FirstName and LastName 
     // fields from our database. If we're not searching then just return the original 
     // input, this speeds things up a bit 
     if ((SearchString == "")) { 
      return InputTxt; 
     } 
     else { 
      // Otherwise create a new regular expression and evaluate the FirstName and 
      // LastName fields against our search string. 
      Regex ResultStr; 
      ResultStr = new Regex(SearchString.Replace(" ", "|"), RegexOptions.IgnoreCase); 
      return ResultStr.Replace(InputTxt, new MatchEvaluator(new System.EventHandler(this.ReplaceWords))); 
     } 
    } 

    public string ReplaceWords(Match m) { 
     // This match evaluator returns the found string and adds it a CSS class I defined 
     // as 'highlight' 
     return ("<span class=highlight>" 
        + (m.ToString + "</span>")); 
    } 

    protected void btnClear_Click(object sender, System.Web.UI.ImageClickEventArgs e) { 
     // Simple clean up text to return the Gridview to it's default state 
     txtSearch.Text = ""; 
     SearchString = ""; 
     Gridview1.DataBind(); 
    } 

    protected void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e) { 
     // Set the value of the SearchString so it gets 
     SearchString = txtSearch.Text; 
    } 
} 

並且這是用於高亮顯示了CSS樣式..

代碼,這是對於上述網格視圖TEH圖像

<style type="text/css"> 
    .highlight {text-decoration: none;color:black;background:yellow;} 
</style> 

我希望它會幫助你...

+0

SQLDatasource如何/在哪裏得到填充新的搜索數據實習生獲取附加到gridview ... ??? – bp581

+0

看到這一行,你也可以使用過濾器表達式 –

0

在databaindg

dsMoodleQuiz.SelectParmeter['FirstName'].DefaultValue = 'John'; 
dsMoodleQuiz.SelectParmeter['LastName'].DefaultValue = 'Wald'; 

gridView1.DataBind(); 

注意使用下面的代碼:我沒有Visual Studio中我,所以代碼是不可複製,pastable。