TextBox文本上的GridView更新已更改

时间:2013-04-17 08:36:28

标签: c# asp.net gridview webforms

我想创建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();
            }

        }
    }

当我输入fname时,GridView没有变化,但当我点击GridView中的selectIt时,它会显示搜索结果。

2 个答案:

答案 0 :(得分:0)

试试这个

if (re != null)
            {

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

答案 1 :(得分:-1)

试试这个

设置网格视图的属性EnableViewState="false"

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