Javascript 在文本框上按键绑定gridview

Javascript 在文本框上按键绑定gridview,javascript,c#,asp.net,Javascript,C#,Asp.net,因此,我已经有了一个gridview,它是根据文本框中输入的带有OnTextChanged事件的数字进行更新的。问题是gridview仅在文本框失去控制时更新,我希望它在根据输入的数字按下某个键时绑定数据。我已经尝试过对web方法的ajax调用,或者从javascript调用代码隐藏函数,但都没有成功 这是我的输入文本框 <asp:UpdatePanel runat="server" ID="upNumComps" UpdateMode="Conditional"> <

因此,我已经有了一个gridview,它是根据文本框中输入的带有OnTextChanged事件的数字进行更新的。问题是gridview仅在文本框失去控制时更新,我希望它在根据输入的数字按下某个键时绑定数据。我已经尝试过对web方法的ajax调用,或者从javascript调用代码隐藏函数,但都没有成功

这是我的输入文本框

<asp:UpdatePanel runat="server" ID="upNumComps" UpdateMode="Conditional">
    <ContentTemplate>
        <table>
            <tr>
                <td>
                    <asp:Label ID="lblNumComps" runat="server" Text="Nº de Componentes " ForeColor="#142658" Style="font-weight: bold;"></asp:Label><span style="color: red; margin-right: 5px;"> * </span>
                </td>
                <td>
                    <asp:TextBox ID="txtNumComps" runat="server" AutoPostBack="True" OnTextChanged="txtNumComps_TextChanged"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvNumComps" runat="server" ForeColor="red" ControlToValidate="txtNumComps" ErrorMessage=""></asp:RequiredFieldValidator>
                </td>
            </tr>
        </table>
    </ContentTemplate>
</asp:UpdatePanel>
编辑

这是我到目前为止管理的最新情况

我添加了这个javascript函数

 <script type="text/javascript">
    function RefreshUpdatePanel(id) {
        debugger
        __doPostBack(id, '');
        document.getElementById(id).blur(id);
        document.getElementById(id).focus(id);
    };
</script>

函数RefreshUpdatePanel(id){
调试器
__doPostBack(id.);
document.getElementById(id).blur(id);
document.getElementById(id).focus(id);
};
在文本框上,我改成了这个

<asp:TextBox ID="txtNumComps" runat="server" onkeyup="RefreshUpdatePanel(this.id);" OnTextChanged="txtNumComps_TextChanged"></asp:TextBox>


但是它做了我想做的,它显示了许多行,这些行是基于在key up上键入的数字,而不是通过点击文本框进行更新,但是仍然存在一个问题,只需按一个键,文本框就会失去焦点,即使我试图通过编程方式设置焦点,它也不起作用,或者它将光标设置在前面键入的数字后面,我不确定您为什么要这样处理它,但我想您可以这样做:

txtNumComps.Attributes.Add(“onKeyUp”、“document.Form1.submit();”


这会激怒大多数用户。

您可以将自定义的
keyup
事件绑定到该文本框。在该事件中,将焦点设置为另一个元素,将触发
TextChanged
事件

<asp:TextBox ID="txtNumComps" runat="server" AutoPostBack="True" OnTextChanged="txtNumComps_TextChanged"></asp:TextBox>

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

<script type="text/javascript">
    $(document).ready(function () {
        $('#<%= txtNumComps.ClientID %>').keyup(function () {
            $('#<%= TextBox1.ClientID %>').focus();
        });
    });
</script>

$(文档).ready(函数(){
$('#').keyup(函数(){
$('#')。焦点();
});
});

但是,使用这种方法,如果用户想要输入
12
,将出现无用的回发。

与我刚才更改的类似,这是我现在试图解决的问题,如何重新聚焦并在键入的数字后显示光标
<asp:TextBox ID="txtNumComps" runat="server" onkeyup="RefreshUpdatePanel(this.id);" OnTextChanged="txtNumComps_TextChanged"></asp:TextBox>
<asp:TextBox ID="txtNumComps" runat="server" AutoPostBack="True" OnTextChanged="txtNumComps_TextChanged"></asp:TextBox>

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

<script type="text/javascript">
    $(document).ready(function () {
        $('#<%= txtNumComps.ClientID %>').keyup(function () {
            $('#<%= TextBox1.ClientID %>').focus();
        });
    });
</script>