asp.net C#的gridview中的搜索问题 [英] Search problem in gridview of asp.net C#

查看:74
本文介绍了asp.net C#的gridview中的搜索问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,


good afternoon,


<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
 <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:TabContainer ID="TabContainer1" runat="server" Width="372px"  ActiveTabIndex="0"  OnClientActiveTabChanged="clientActiveTabChanged" >
            <asp:TabPanel ID="test" runat="server" HeaderText="panel">
            <ContentTemplate>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" 

                 

           

            <table>
            <tr>
            <td><asp:Label ID="lblSearch" runat="server" Text="Search"></asp:Label></td>
            <td><asp:TextBox ID="txtSearch" runat="server"></asp:TextBox></td>
            <td>
                <asp:Button ID="btnSearch" runat="server" Text="Search"

                    onclick="btnSearch_Click" /></td>
            </tr>
            </table>
             <asp:GridView ID="TestGrid" runat="server" AutoGenerateColumns="False"

                    DataKeyNames="ItemID" DataSourceID="SqlDataSource1" Height="141px"

                    Width="334px"  >
             <Columns>
                 <asp:TemplateField HeaderText="Item Id">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server"  Text='<%# Bind("ItemID") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                     <asp:TemplateField HeaderText="ItemDescription">
                     <ItemTemplate>
                    <asp:LinkButton ID="LinkEdit" runat="server" Text='<%# Bind("ItemDescription") %>' CommandArgument='<%# Bind("ItemID")%>' OnClick="LinkEdit_OnClick"  ></asp:LinkButton>
                </ItemTemplate>
                 </asp:TemplateField>
                 <asp:BoundField DataField="Remarks" HeaderText="Remarks"

                     SortExpression="Remarks"  />
             </Columns>
              </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server"

                    ConnectionString="<%$ ConnectionStrings:DeemahTestConnectionString %>"

                    SelectCommand="SELECT [ItemID], [ItemDescription], [Remarks] FROM [ItemMaster]">
                </asp:SqlDataSource>
                </ContentTemplate>
                </asp:UpdatePanel>
            </ContentTemplate>
            </asp:TabPanel>





我在文件后面的代码如下.





and my code behind file is given below..

protected void btnSearch_Click(object sender, EventArgs e)
      {
          string connectionString = (string)ConfigurationManager.ConnectionStrings["DeemahTestConnectionString"].ConnectionString;
          ConnDeemah.ConnectionString = connectionString;
          string sqlQuery = "select top(1) * from itemMaster where ItemDescription like ''%+txtSearch.Text+%'' ";
          SqlCommand cmd = new SqlCommand(sqlQuery, ConnDeemah);
          ConnDeemah.Open();
          SqlDataReader rdr = null;
          rdr = cmd.ExecuteReader();

          ConnDeemah.Close();
          TestGrid.DataSource = rdr.ToString();
          TestGrid.DataBind();
      }


当我运行此页面时,它给我错误

"DataSource和DataSourceID都在"TestGrid"上定义.删除一个定义."


所以请给我我可以通过文本框在gridview中搜索的方式

谢谢

[edit]添加了前置标记


when i run this page its give me the error

"Both DataSource and DataSourceID are defined on ''TestGrid''. Remove one definition."


so please give me the way which i can do searching in gridview by textboxes

thanks

[edit]added pre tag

推荐答案

ConnectionStrings:DeemahTestConnectionString %> " SelectCommand =" > < /asp:SqlDataSource > < /ContentTemplate > < /asp:UpdatePanel > < /ContentTemplate > < /asp:TabPanel >
ConnectionStrings:DeemahTestConnectionString %>" SelectCommand="SELECT [ItemID], [ItemDescription], [Remarks] FROM [ItemMaster]"> </asp:SqlDataSource> </ContentTemplate> </asp:UpdatePanel> </ContentTemplate> </asp:TabPanel>





我在文件后面的代码如下.





and my code behind file is given below..

protected void btnSearch_Click(object sender, EventArgs e)
      {
          string connectionString = (string)ConfigurationManager.ConnectionStrings["DeemahTestConnectionString"].ConnectionString;
          ConnDeemah.ConnectionString = connectionString;
          string sqlQuery = "select top(1) * from itemMaster where ItemDescription like ''%+txtSearch.Text+%'' ";
          SqlCommand cmd = new SqlCommand(sqlQuery, ConnDeemah);
          ConnDeemah.Open();
          SqlDataReader rdr = null;
          rdr = cmd.ExecuteReader();

          ConnDeemah.Close();
          TestGrid.DataSource = rdr.ToString();
          TestGrid.DataBind();
      }


当我运行此页面时,它给我错误

"DataSource和DataSourceID都在"TestGrid"上定义.删除一个定义."


所以请给我我可以通过文本框在gridview中搜索的方式

谢谢

[edit]添加了前置标记


when i run this page its give me the error

"Both DataSource and DataSourceID are defined on ''TestGrid''. Remove one definition."


so please give me the way which i can do searching in gridview by textboxes

thanks

[edit]added pre tag


//you are Binding your DataGrid with SqlDataSource and from Code behind also 
//Remove One binding from your Code 

//From Code Behind 

string connectionString = (string)ConfigurationManager.ConnectionStrings["DeemahTestConnectionString"].ConnectionString;
ConnDeemah.ConnectionString = connectionString;
string sqlQuery = "select top(1) * from itemMaster where ItemDescription like ''%+txtSearch.Text+%'' ";
SqlCommand cmd = new SqlCommand(sqlQuery, ConnDeemah);
ConnDeemah.Open();
SqlDataReader rdr = null;
rdr = cmd.ExecuteReader();

ConnDeemah.Close();
TestGrid.DataSource = rdr.ToString();
TestGrid.DataBind();

//Or from Aspx Page


<asp:sqldatasource id=""SqlDataSource1"" runat=""server"<br" mode="hold" xmlns:asp="#unknown" />                    ConnectionString="<%


ConnectionStrings:DeemahTestConnectionString %>" SelectCommand="SELECT [ItemID], [ItemDescription], [Remarks] FROM [ItemMaster]"> </asp:SqlDataSource>
ConnectionStrings:DeemahTestConnectionString %>" SelectCommand="SELECT [ItemID], [ItemDescription], [Remarks] FROM [ItemMaster]"> </asp:SqlDataSource>


这篇关于asp.net C#的gridview中的搜索问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆