2015-12-15 140 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(); }

相关问题