如何在TextChange事件上调用TextBox回发而不改变焦点?

时间:2011-02-10 12:39:18

标签: c# asp.net

情绪化: 每次用户在TextBox控件中键入字母时,我都需要在Ajax UpdatePanel中对gridview进行数据绑定。如何覆盖TextChange事件以强制它为我做回发? 谢谢!

<script type="text/javascript">

    function ace1_itemSelected(sender, e) {
        var hdCustID = $get('<%= hdCustID.ClientID %>');
        hdCustID.value = e.get_value();
    }

</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="lblCustName" Text="Cust Name:" AssociatedControlID="txtCustName" runat="server" />
        <asp:TextBox ID="txtCustName" AutoPostBack="false" AutoComplete="Off" runat="server" OnTextChanged="txtCustName_TextChanged" />
        <ajaxToolkit:AutoCompleteExtender id="ace1" targetcontrolid="txtCustName" ServicePath="~/WebServices/FoodItemSuggestionService.asmx" servicemethod="FindTitles"
            minimumprefixlength="1" OnClientItemSelected="ace1_itemSelected" firstrowselected="false" runat="server" />
        <asp:HiddenField ID="hdCustID" runat="server" />
        <br />
        <br />
        <br />
        <asp:GridView ID="gvCustomer" runat="server" AutoGenerateColumns="False" CellPadding="4"
            ForeColor="#333333" GridLines="None" PageSize="4" BorderStyle="Groove">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
                <asp:BoundField DataField="Title" HeaderText="Название" SortExpression="Title" />
                <asp:BoundField DataField="CategoryTitle" HeaderText="Категория" SortExpression="CategoryTitle" />
            </Columns>
            <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

protected void txtCustName_TextChanged(object sender, EventArgs e)
    {
        try
        {
            gvCustomer.DataSource = FoodItemBO.FindFoodItems(hdCustID.Value);
            gvCustomer.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

<强> JQUERY:

$("#txtName").keydown(function(){
    $("#form1").submit();
});