[英]GridView update on TextBox text changed
我想為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" />
<asp:TextBox ID="job" runat="server" Width="94px" />
<asp:TextBox ID="username" runat="server" Width="98px" />
<asp:TextBox ID="lname" runat="server" Width="123px" />
<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();
}
}
}
當我輸入fname
, GridView
不會改變,但是當我在GridView
單擊selectIt時,它會顯示搜索結果。
嘗試這個
if (re != null)
{
res.DataSource = re;
res.DataBind();
}
嘗試這個
設置網格視圖的屬性EnableViewState="false"
<asp:GridView ID="res" runat="server" EnableViewState="False" >
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.