Asp.net 数据绑定GridView OnKeyUp/Down

Asp.net 数据绑定GridView OnKeyUp/Down,asp.net,Asp.net,我有一个gridview,可以显示客户信息。我希望它在用户在文本框中键入名字和/或姓氏时更新结果。基本上,当用户键入时,使用文本框中的更新字符将gridview数据绑定到其数据源 代码编辑: <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Process.aspx.cs" Inherits="Reservations.WebForm3" %&g

我有一个gridview,可以显示客户信息。我希望它在用户在文本框中键入名字和/或姓氏时更新结果。基本上,当用户键入时,使用文本框中的更新字符将gridview数据绑定到其数据源

代码编辑:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Process.aspx.cs" Inherits="Reservations.WebForm3" %>

<asp:UpdateProgress ID="UpdateProgress1" runat="server" 
        AssociatedUpdatePanelID="UpdatePanel1">
        <ProgressTemplate>Processing...</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
      <asp:TextBox ID="firstname" runat ="server" class="inputLarge" ontextchanged="firstname_TextChanged" />
      <asp:TextBox ID="lastname" runat="server" class="inputLarge" />

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="457px">
                <Columns>
                    <asp:TemplateField>
                        <EditItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>
                    <asp:BoundField DataField="CustID" HeaderText="Cust ID">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="FirstName" HeaderText="First Name">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="LastName" HeaderText="Last Name">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="City" HeaderText="City">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                </Columns>
            </asp:GridView>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Scripts></Scripts>
            </asp:ScriptManager>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="firstname" EventName="TextChanged" />
        </Triggers>
    </asp:UpdatePanel>


protected void firstname_TextChanged(object sender, EventArgs e)
    {
        InventoryAppSoapClient inst = new InventoryAppSoapClient();
        DataSet ds = inst.getCustomer(firstname.Text, "none");
        GridView1.DataSource = ds;
        GridView1.DataBind();           
    }

处理。。。
受保护的void firstname_TextChanged(对象发送方,事件参数e)
{
InventoryAppSoapClient inst=新的InventoryAppSoapClient();
数据集ds=inst.getCustomer(firstname.Text,“无”);
GridView1.DataSource=ds;
GridView1.DataBind();
}

除了TextChanged事件,其他一切都可以第一次运行。如果我返回并更改文本并关闭选项卡,则不会发生任何事情。

您是否尝试为文本框的OnTextChanged事件添加事件处理程序

在里面你可以像这样强制数据绑定

Private void firstName_OnTextChanged(object sender, EventArgs e)
{
//Your datasource and parameters are already defined on the front end so there is no 
//setup required
 existing.DataBind();
}
文本框lastName也是如此

Private void lastName_OnTextChanged(object sender, EventArgs e)
{
//Your datasource and parameters are already defined on the front end so there is no 
//setup required
 existing.DataBind();
}
别忘了为前端的每个文本框将方法添加到OnTextChange属性中。像这样

<asp:TextBox ID="firstname" OnTextChanged="firstName_OnTextChanged" runat ="server" class="inputLarge"/>
<asp:TextBox ID="lastname" OnTextChanged="lastName_OnTextChanged" runat="server" class="inputLarge" />


此外,如果将此代码嵌入更新面板并使用AJAX控制工具包(免费),网格将无缝更新。否则,如果您使用经典的asp技术,它将不会很漂亮。每次按一个键,该页面都将继续发回。这会让人恼火。

我已经想到了这一点,但我希望网格随着用户类型的变化而更新。直到文本框失去焦点,ContextChanged才会触发。是否无法使用onKeyUp和onKeyDown事件在javascript或JQuery中绑定?我正在尝试您的建议,但gridview没有响应firstname_OnTextChanged()事件中现有的.DataBind()调用。我在配置数据源时测试了查询,所以我知道它应该返回一个结果。是不是因为gridview最初是空的,所以比较麻烦?如果您使用的是更新面板,我需要完整地查看HTML标记,因为您可能需要声明一个异步触发器。确实如此。我已经把它带到了一切正常的地方,除了ContextChanged事件只在第一次触发。如果我回到文本框,第二次更改文本并关闭选项卡,什么也不会发生。我会更新我的代码。你也添加了@page指令吗?您正在使用ajax控件工具包吗?