【问题标题】:DataBind GridView OnKeyUp/DownDataBind GridView OnKeyUp/Down
【发布时间】:2012-01-24 21:42:42
【问题描述】:

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

代码编辑:

<%@ 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 事件之外,一切都只在第一次出现。如果我返回并更改文本并关闭标签,则不会发生任何事情。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    您是否尝试为文本框的 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();
    }
    

    你的文本框姓氏也是如此

    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 技术,它将不会很漂亮。每次您按下一个键时,该页面都会继续发回。会很烦的。

    【讨论】:

    • 我已经想到了,但我希望网格在用户键入时更新。在文本框失去焦点之前,onTextChanged 不会触发。有没有办法使用 onKeyUp 和 onKeyDown 事件在 javascript 或 JQuery 中进行绑定?
    • 我正在尝试您的建议,但 gridview 没有响应 firstname_OnTextChanged() 事件中的 existing.DataBind() 调用。我在配置数据源时测试了查询,所以我知道它应该返回结果。是不是因为gridview最初是空的而挣扎?
    • 如果您使用的是更新面板,我需要完整查看 HTML 标记,因为您可能必须声明异步触发器。
    • 是的。除了 ontextchanged 事件仅在第一次触发之外,我已经将它带到了一切正常的地方。如果我回到文本框,第二次更改文本和制表符,没有任何反应。我会更新我的代码
    • 你能添加你的@page指令吗?你在用ajax控制工具包吗?
    猜你喜欢
    • 2010-10-03
    • 2021-07-20
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    相关资源
    最近更新 更多