1

我想为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它会显示搜索结果。

4

2 回答 2

0

尝试这个

if (re != null)
            {

                res.DataSource = re;
                res.DataBind();
            }
于 2013-04-17T09:05:00.860 回答
-1

尝试这个

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

<asp:GridView ID="res" runat="server" EnableViewState="False" >
于 2013-04-17T11:00:35.003 回答