2017-05-30 47 views
1

我在一邊發現了我的問題的實現,但我不知道爲什麼它不工作。當我把一些值放入文本框時,它應該爲我做一個回發,但它不會。AJAX asp Web表格在網格中搜索

<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

請改變'內的'到'的' TextBox的ID不匹配。 – Rainmaker

回答

0

文本框的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(); 
      } 
     } 
+0

你有權利,但它仍然無法工作 – Adriano

0

我建議更新AsyncPostBackTrigger作爲跟隨到textbox = TextBox1中的ID與AutoPostBack參考匹配,但該控制需要是外UpdatePanel

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

我也建議嘗試使用PostBackTrigger來代替。這是主要用於控制UpdatePanel,使一個完整訊息背面

<asp:PostBackTrigger ControlID="TextBox1" /> 
+0

'PostBackTrigger'運作良好,但我不知道爲什麼第一個'AsyncPostBackTrigger'不 – Adriano

+0

'PostBackTrigger'仍然重新加載我的所有頁面 – Adriano

+0

從以前的經驗是權衡AutoPostBack上班。我不知道這個行爲的修復是什麼。 –