2013-04-17 101 views
1

我想爲GridView數據創建一個實時搜索,所以我嘗試這些代碼:在文本框文字的GridView更新改變

aspx文件中有這樣的:

 <asp:ScriptManager ID="DateManager" runat="server" /> 
      <asp:UpdatePanel ID="setDate" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true"> 
       <ContentTemplate> 
       <asp:TextBox ID="gro" runat="server" Width="73px" />&nbsp; 
       <asp:TextBox ID="job" runat="server" Width="94px" />&nbsp; 
       <asp:TextBox ID="username" runat="server" Width="98px" />&nbsp; 
       <asp:TextBox ID="lname" runat="server" Width="123px" />&nbsp; 
       <asp:TextBox ID="fname" runat="server" Width="78px" 
         ontextchanged="fname_TextChanged" AutoPostBack="True" /> 
      <asp:GridView ID="res" runat="server" AutoGenerateColumns="False" 
       CellPadding="4" ForeColor="#333333" GridLines="None" Width="600px" 
         AllowPaging="True" DataKeyNames="id"> 
       <AlternatingRowStyle BackColor="White" /> 
       <Columns> 
       <asp:CommandField SelectText="selectIt" ShowSelectButton="True" /> 
        <asp:BoundField DataField="fname" HeaderText="First Name" /> 

       </Columns> 
       <EditRowStyle BackColor="#2461BF" /> 
       <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
       <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
       <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
       <RowStyle BackColor="#EFF3FB" /> 
       <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
       <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
       <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
       <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
       <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
      </asp:GridView> 

      </ContentTemplate> 
       <Triggers> 
        <asp:AsyncPostBackTrigger ControlID="fname" EventName="TextChanged" /> 
       </Triggers> 
      </asp:UpdatePanel> 

,並在後面的代碼:

protected void fname_TextChanged(object sender, EventArgs e) 
    { 
     using (HotelEntities h = new HotelEntities()) 
     { 
      UserHandle me = new UserHandle(); 

      var re = me.SearchFName(h, fname.Text); 

      if (re != null) 
      { 
       DataTable d = Converter.UserForSearch(re); 
       res.DataSource = d; 
       res.DataBind(); 
      } 

     } 
    } 

當我輸入fnameGridView不變,但當我點擊selectIt在GridView它顯示搜索結果。

回答

0

試試這個

if (re != null) 
      { 

       res.DataSource = re; 
       res.DataBind(); 
      } 
-1

試試這個

網格視圖設置屬性EnableViewState="false"

<asp:GridView ID="res" runat="server" EnableViewState="False" >