2012-01-24 29 views
0

我有一个显示客户信息的gridview。我希望它在用户输入第一个和/或最后一个名称到文本框中时更新结果。基本上,将数据绑定到它的数据源,并使用用户键入的文本框中的更新字符。DataBind GridView OnKeyUp/Down

CODE编辑:

<%@ 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事件仅第一的第一次。如果我返回并更改文本和标签,则什么也不会发生。

回答

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属性为每个文本框前端。像这样。此外,如果您将此代码嵌入更新面板并使用AJAX控件工具包(免费),网格将无缝更新。否则,如果你使用传统的asp技术,它不会很漂亮。每次按下某个键时,该页面都会继续回复。它会变得烦人。

+0

我想到了这一点,但我希望网格更新为用户类型。 onTextChanged不会触发,直到文本框失去焦点。有没有办法使用onKeyUp和onKeyDown事件绑定在JavaScript或JQuery? – jmease

+0

我正在尝试你的建议,但gridview没有响应firstname_OnTextChanged()事件中的existing.DataBind()调用。我在配置数据源时测试了查询,所以我知道它应该返回结果。它可能是挣扎,因为gridview最初是空的? – jmease

+0

您的gridview是否在更新面板中? – gsirianni