如何从表中搜索记录并在c#中的gridview中显示?

时间:2015-12-15 05:35:42

标签: c# asp.net

我是.net的新手,我已经创建了文本框,下拉列表和searc按钮。

当我在下拉列表中选择“以...开头”并在文本框中键入一些字符并单击搜索按钮时,它应该在gridview中的表格中显示记录(根据搜索文本)。

为此,我创建了下拉列表,并创建了存储过程。我添加了以下代码:

这是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 '%' +@searchtext
else
select * from student where name like '%' +@searchtext+ '%'
End

当我运行此代码时,它会显示以下错误:

  

编译器错误消息:CS1061:'ASP.searchrecords_aspx'不包含'GridView1_RowCommand'的定义,并且没有扩展方法'GridView1_RowCommand'接受类型'ASP.searchrecords_aspx'的第一个参数可以找到(你是否错过了使用指令或程序集引用?)

我正在努力解决这个问题,我知道,如何在我的代码中传递这两个@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

3 个答案:

答案 0 :(得分:0)

在您的ASPX中为OnRowCommand声明了GridViewGridView1_RowCommand事件。但它没有在你的代码中定义。从ASPX中删除OnRowCommand="GridView1_RowCommand"事件。或者将该事件添加到后面的代码中。

答案 1 :(得分:0)

您的代码首先遇到两个问题:您是否已使用OnRowCommand="GridView1_RowCommand"注册了 RowCommand 事件,但您尚未定义它,因此要么必须在代码后面处理它,要么从gridview中删除该属性。

第二个问题是你使用了一个SQLDataSource控件来触发SP sp_searchedstudentrecords,它需要两个参数,即@condition&amp; @searchtext但是你没有从你的控制中传递任何一个。我想你想从你的下拉列表和文本框控件中传递这些值。然后改变你的代码: -

<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>

答案 2 :(得分:0)

首先,您要更改后面代码中的参数名称, 因为查询和代码背后的参数名称必须相同..

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( “@条件”,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();         }