2015-12-15 67 views
0

我是新來的.NET,我已創建文本框,下拉列表和變奏按鈕。如何從表和顯示搜索記錄在GridView控件在C#?

當我選擇「開頭」,在下拉,並在文本框中鍵入一些字符,然後單擊搜索按鈕,它應該顯示的記錄(根據搜索文本)在GridView控件從表中。

對於我創建下拉,並創建存儲procedure.And我添加以下代碼:

下面是ASPX:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
<td><asp:HiddenField ID="hiddenfield" runat="server" /> 
<td><asp:HiddenField ID="curricular" runat="server" /> 
    <asp:TextBox ID="searchid" runat="server" Visible="false"></asp:TextBox> 
</td> 
    <td> 

    <asp:Label ID="condition" runat="server" Visible="false" > 
    </asp:Label> 
    <asp:Label ID="searchtext" runat="server" Visible="false"></asp:Label> 
    </td>  
<asp:DropDownList ID="searchrecord" runat="server" Width="150px"></asp:DropDownList> 
<asp:TextBox ID="textsearch" runat="server"></asp:TextBox> 
<asp:Button ID="searchclick" runat="server" text = "search" OnClick="searchrecords_Click" OnClientClick="return searchrecords();" /> 
    <center><div><h4>Searched Records</h4></div></center><br /> <br /> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataKeyNames="ID" DataSourceID="SqlDataSource1" 
     OnRowCommand="GridView1_RowCommand" 
     EnablePersistedSelection="True" BackColor="White" 
     OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="240px" 
     Width="755px"> 
     <Columns> 
      <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
       ReadOnly="True" SortExpression="ID" /> 
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
      <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" /> 
      <asp:BoundField DataField="Section" HeaderText="Section" 
       SortExpression="Section" /> 
      <asp:BoundField DataField="Address" HeaderText="Address" 
       SortExpression="Address" /> 
       <asp:BoundField DataField="Email" HeaderText="Email" 
       SortExpression="Email" /> 
      <asp:BoundField DataField="Mobilenum" HeaderText="Mobile Number" 
       SortExpression="Mobile Number" /> 
       <asp:ImageField DataImageUrlField="Image" HeaderText="Image" ControlStyle-Width="50" ControlStyle-Height = "50">     
       <ControlStyle Height="50px" Width="50px"></ControlStyle> 
       </asp:ImageField> 
       <asp:BoundField DataField="Extracurricular" HeaderText="Extracurricular" 
       SortExpression="Name" />    
     </Columns> 
     <HeaderStyle BackColor="#FF0066" BorderColor="#CCFFFF" ForeColor="White" 
      Height="50px" Width="50px" /> 
     <SelectedRowStyle BackColor="#FF66FF" /> 
    </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="sp_searchedstudentrecords"   
     SelectCommandType="StoredProcedure"> 

    </asp:SqlDataSource> 

    <script type="text/javascript"> 
     function searchrecords() { 
      if ($.trim(document.getElementById("<%=textsearch.ClientID%>").value).length == 0) { 
       alert("Enter Your Characters to search !"); 
       document.getElementById("<%=textsearch.ClientID%>").focus(); 
       return false; 
      } 
     } 
</script> 
    <script type="text/javascript" src="Scripts/jquery-2.1.4.js" /> 

</asp:Content> 

這裏是我的代碼隱藏:

protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       SqlConnection con = Connection.DBconnection(); 
       { 
        SqlCommand com = new SqlCommand("sp_studentsearchrecords", con); 
        com.CommandType = CommandType.StoredProcedure; 
        com.Parameters.AddWithValue("@id", searchid.Text.Trim()); 
        com.Parameters.AddWithValue("@searchrecords", searchrecord.Text.Trim()); 

        SqlDataAdapter adp = new SqlDataAdapter(com); 
        DataSet ds = new DataSet(); 
        adp.Fill(ds); 

        if (ds.Tables[0].Rows.Count > 0) 
        { 
         searchrecord.DataSource = ds; 
         searchrecord.DataTextField = "searchrecords"; 
         searchrecord.DataValueField = "id"; 
         searchrecord.DataBind(); 
        } 
       } 
      } 
     } 
     protected void searchrecords_Click(object sender, EventArgs e) 
     { 
      SqlConnection con = Connection.DBconnection(); 
      { 
       SqlCommand com = new SqlCommand("sp_insertsearchtext", con); 
       com.CommandType = CommandType.StoredProcedure; 
       com.Parameters.AddWithValue("@searchname", searchtext.Text.Trim()); 
       com.ExecuteNonQuery(); 
      } 
     } 
     protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e) 
     { 
      int index = GridView1.SelectedIndex; 
      hiddenfield.Value = index.ToString(); 
     } 

最後的存儲過程:

ALTER PROCEDURE sp_searchedstudentrecords 
(
@condition varchar(20), 
@searchtext varchar(50) 
) 
AS 
begin 
If (@condition = 'startswith') 
select * from student where name like @searchtext+ '% ' 
else if (@condition = 'endswith') 
select * from student where name like '%' [email protected] 
else 
select * from student where name like '%' [email protected]+ '%' 
End 

當我運行這段代碼,它顯示了以下錯誤:

Compiler Error Message: CS1061: 'ASP.searchrecords_aspx' does not contain a definition for 'GridView1_RowCommand' and no extension method 'GridView1_RowCommand' accepting a first argument of type 'ASP.searchrecords_aspx' could be found (are you missing a using directive or an assembly reference?)

我竭力要解決這個問題,我可以知道,如何在我的代碼通過這兩個@condition@searchtext參數?

任何幫助將不勝感激。

感謝,

更新:

protected void searchrecords_Click(object sender, EventArgs e) 
     { 
      SqlConnection con = Connection.DBconnection(); 
      { 
       SqlCommand com = new SqlCommand("sp_insertsearchtext", con); 
       com.CommandType = CommandType.StoredProcedure; 
       com.Parameters.AddWithValue("@id", idsearch.Text.Trim()); 
       com.Parameters.AddWithValue("@searchtext", searchtext.Text.Trim());    
       com.ExecuteNonQuery(); 
      } 
     } 
     protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e) 
     { 
      int index = GridView1.SelectedIndex; 
      hiddenfield.Value = index.ToString(); 
     } 

ALTER PROCEDURE sp_insertsearchtext 
    (
    @id int, 
@searchtext Varchar (100) 
) 
AS 
begin 
Insert into searchtext (searchtext) values (@searchtext) 
End 
+0

從ASPX中刪除OnRowCommand =「GridView1_RowCommand」。或者將相關事件添加到代碼後面。 –

回答

0

你有兩個問題,你的代碼第一個是您已經註冊的RowCommand使用OnRowCommand="GridView1_RowCommand"事件,但你有沒有這樣無論是你將不得不在代碼來處理它後面或者從GridView中刪除屬性定義它。

第二個問題是你用了SQLDataSource控件來觸發SP sp_searchedstudentrecords並需要兩個參數,即@condition & @searchtext但你從你的控制通過他們沒有。我想你想要從你的下拉列表和文本框控件resp中傳遞這些值。然後改變你的代碼是這樣的: -

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="sp_searchedstudentrecords"   
    SelectCommandType="StoredProcedure"> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="searchrecord" Name="condition" 
      PropertyName="SelectedValue" Type="String" /> 
     <asp:ControlParameter ControlID="searchtext" Name="searchtext" PropertyName="Text" 
      Type="String" /> 
    </SelectParameters> 
</asp:SqlDataSource> 
+0

我會檢查你的答案,並將更新你,謝謝 – pcs

+0

現在沒有顯示錯誤..但沒有顯示記錄在gridview後單擊搜索按鈕。 – pcs

+0

你能查看我更新的帖子嗎? – pcs

0

。在你的ASPX爲GridView聲明的OnRowCommand事件稱爲GridView1_RowCommand。但是它沒有在你的代碼背後定義。卸下ASPX的OnRowCommand="GridView1_RowCommand"事件。或者將該事件添加到後面的代碼中。

+0

我聽不懂..你能詳細說說嗎?謝謝 – pcs

+0

@Rani回答更新。 – Irshad

+0

如果我刪除OnRowCommand =「GridView1_RowCommand」..rest的行顯示爲錯誤。 – pcs

0

起初,你想在你的代碼更改參數的名稱背後, 因爲參數名稱必須是在您的查詢,後面的代碼相同..

保護無效searchrecords_Click(對象發件人,EventArgs的) { 的SqlConnection CON =連接。DBConnection的(); SqlCommand com = new SqlCommand(「sp_insertsearchtext」,con); com.CommandType = CommandType.StoredProcedure; com.Parameters.AddWithValue(「@ condition」,idsearch.Text.Trim()); com.Parameters.AddWithValue(「@ searchtext」,searchtext.Text.Trim());
com.ExecuteNonQuery(); } } protected void GridView1_SelectedIndexChanged(Object sender,EventArgs e) { int index = GridView1.SelectedIndex; hiddenfield.Value = index.ToString(); }