DataBind GridView OnKeyUp / Down

时间:2012-01-24 21:42:42

标签: asp.net

我有一个显示客户信息的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();           
    }

除了TextChanged事件之外,所有内容都是第一次有效。如果我回去更改文本和标签,则没有任何反应。

1 个答案:

答案 0 :(得分:0)

您是否尝试为文本框的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技术,它将不会很好。每次按键时页面都会继续回发。这会很烦人。