我发现问题的执行方面,但我不知道为什么它不起作用。当我在文本框中加入一些值时,它应该给我一个回发,但事实并非如此。
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txt" />
</Triggers>
<ContentTemplate>
<asp:TextBox runat="server" ID="TextBox1" AutoPostBack="true" OnTextChanged="txt_TextChanged"></asp:TextBox>
<asp:GridView runat="server" ID="GridView2">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ItemStyle-Font-Size="10px" />
<asp:BoundField DataField="regon" HeaderText="Regon" SortExpression="regon" ItemStyle-Font-Size="10px" />
<asp:BoundField DataField="nip" HeaderText="NIP" SortExpression="nip" ItemStyle-Font-Size="10px" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txt.Attributes.Add("onkeyup", "javascript:setTimeout('__doPostBack(\'txt\',\'\')', 0)");
string SelectCommand = "SELECT * " +
" FROM client_inf WHERE amount > 1000";
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(SelectCommand, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();
}
}
protected void txt_TextChanged(object sender, EventArgs e)
{
if (txt.Text != "")
{
string SelectCommand = "SELECT * " +
" FROM client_inf WHERE client_name Like '" + txt.Text + "%'"
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(SelectCommand, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();
}
}
http://www.infosearchshop.com/21-gridview-search-as-you-type-with-ajax
答案 0 :(得分:0)
TextBox的ID和 GridView的ID 不匹配。
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txt" />
</Triggers>
<ContentTemplate>
<asp:TextBox runat="server" ID="txt" AutoPostBack="true" OnTextChanged="txt_TextChanged"></asp:TextBox>
<asp:GridView runat="server" ID="GridView1">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ItemStyle-Font-Size="10px" />
<asp:BoundField DataField="regon" HeaderText="Regon" SortExpression="regon" ItemStyle-Font-Size="10px" />
<asp:BoundField DataField="nip" HeaderText="NIP" SortExpression="nip" ItemStyle-Font-Size="10px" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txt.Attributes.Add("onkeyup", "javascript:setTimeout('__doPostBack(\'txt\',\'\')', 0)");
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
}
private DataTable GetDataSource()
{
var dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("regon", typeof(string));
dt.Columns.Add("nip", typeof(string));
dt.Rows.Add("Name-1", "regon-1", "nip-1");
dt.Rows.Add("Name-2", "regon-1", "nip-1");
dt.Rows.Add("Name-3", "regon-1", "nip-1");
dt.Rows.Add("Name-4", "regon-1", "nip-1");
dt.Rows.Add("Name-5", "regon-1", "nip-1");
dt.Rows.Add("Name-6", "regon-1", "nip-1");
dt.Rows.Add("Name-7", "regon-1", "nip-1");
return dt;
}
protected void txt_TextChanged(object sender, EventArgs e)
{
if (txt.Text != "")
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
}
答案 1 :(得分:0)
我建议更新AsyncPostBackTrigger
,如下所示,将textbox
= TextBox1的ID与AutoPostBack
引用相匹配,但控件必须在UpdatePanel
之外
<asp:AsyncPostBackTrigger ControlID ="TextBox1" EventName ="TextChanged" />
我还建议您尝试使用PostBackTrigger
。这主要用于UpdatePanel
内部完全回发的控件
<asp:PostBackTrigger ControlID="TextBox1" />