使用gridview绑定搜索文本框 [英] Bind search textbox with gridview
问题描述
您好,
我正在尝试使用下面的代码搜索我的网格视图,当我在文本中输入值时,我无法使用gridview来过滤结果客户端的方框。
非常感谢任何帮助。
谢谢。
Hello,
I am trying search my grid view using the following code below and I cannot get the gridview to filter results when I type in a value in text box on the client-side.
Any help would be very much appreciated.
Thank you.
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
String constr = ConfigurationManager.ConnectionStrings["cdwConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("select [description], [role] from [dbo].[login] where role = @role");
cmd.Connection = con;
cmd.Parameters.Add("@role", SqlDbType.NVarChar).Value = TextBox1.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
//TextBox1.Text = "";
}
}
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="search" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Download Data" onclick="btnExportToExcel_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" onrowdatabound="gvrecords_RowDataBound">
<Columns>
<asp:BoundField DataField="description" HeaderText="UserName" />
<asp:BoundField DataField="role" HeaderText="FirstName" />
</Columns>
</asp:GridView>
推荐答案
if (TextBox1.Text == "")
试试这个..
try this..
if (TextBox1.Text != "")
试试这个
Try this
if (TextBox1.Text != String.Empty)
在.Net pre 2.0中,创建一个对象,而String.Empty不创建任何对象。因此使用String.Empty更有效。
In .Net pre 2.0, "" creates an object while String.Empty creates no object. So it is more efficient to use String.Empty.
信息来源
.Length == 0是最快的选项,但是.Empty会使代码稍微清晰。
所以相当于.Empty,但仍然没有那么快as .Length == 0.
Source of information
.Length == 0 is the fastest option, but .Empty makes for slightly cleaner code.
So "" is pretty equivalent to .Empty, but still not as fast as .Length == 0.
更改条件: -
change your condition:-
if (TextBox1.Text == "")
要
To
if (TextBox1.Text != "")
然后尝试仍然没有解决。然后用改进的问题回复。
then try still not solved. then revert back with improved question.
这篇关于使用gridview绑定搜索文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!