【问题标题】:AJAX asp Web Forms search in grid网格中的 AJAX asp Web 表单搜索
【发布时间】:2023-03-23 22:13:01
【问题描述】:

我发现了我的问题的实现,但我不知道为什么它不起作用。当我在文本框中输入一些值时,它应该给我一个回发,但它没有。

<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

【问题讨论】:

  • 请将 &lt;asp:AsyncPostBackTrigger ControlID="txt" /&gt; 更改为 &lt;asp:AsyncPostBackTrigger ControlID="TextBox1" /&gt; TextBox 的 id 不匹配。

标签: c# jquery asp.net ajax


【解决方案1】:

我建议更新AsyncPostBackTrigger 如下以匹配textbox = TextBox1 的ID 与AutoPostBack 引用,但控件需要在UpdatePanel 之外

<asp:AsyncPostBackTrigger ControlID ="TextBox1" EventName ="TextChanged" />

我还建议尝试改用PostBackTrigger。这主要用于UpdatePanel 中的控件,它可以进行完整的回帖

<asp:PostBackTrigger ControlID="TextBox1" />

【讨论】:

  • PostBackTrigger 效果很好,但我不知道为什么第一个 AsyncPostBackTrigger 没有
  • PostBackTrigger 仍然重新加载我的所有页面
  • 从以前的经验来看,这是 AutoPostBack 工作的权衡。我不知道这种行为的解决方法是什么。
【解决方案2】:

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();
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多