如何在KeyUp上创建文本框回发?

Rus*_*rry 24 asp.net ajax postback updatepanel asp.net-ajax

我有一个文本框,可以更改OnTextChanged事件中下拉列表的内容.当文本框失去焦点时,此事件似乎会触发.如何在按键或键盘事件中实现此目的?

这是我的代码示例

<asp:TextBox ID="Code" runat="server" AutoPostBack="true" OnTextChanged="Code_TextChanged">                

<asp:UpdatePanel ID="Update" runat="server">
    <ContentTemplate>
        <asp:DropDownList runat="server" ID="DateList" />             
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Code" />
    </Triggers>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

所以在代码隐藏中,我在页面加载时绑定下拉列表.Code_TextChanged事件只是重新绑定下拉列表.我希望在每个按键上发生这种情况,而不是在文本框失去焦点时发生.

我最近继承了这个代码,这不是我这样做的理想方法,但是时间限制阻止我在web servicy方法中重写它.

我已经尝试使用jQuery来绑定"keyup"事件以匹配文本框的"更改"事件,但这仅适用于按下的第一个键.

Cod*_*awk 43

这将解决您的问题.逻辑与凯尔建议的解决方案相同.

看看这个.

<head runat="server">
<title></title>
<script type="text/javascript">
    function RefreshUpdatePanel() {
        __doPostBack('<%= Code.ClientID %>', '');
    };
</script>

    <asp:TextBox ID="Code" runat="server" onkeyup="RefreshUpdatePanel();" AutoPostBack="true" OnTextChanged="Code_TextChanged"></asp:TextBox>
    <asp:UpdatePanel ID="Update" runat="server">
        <ContentTemplate>
            <asp:DropDownList runat="server" ID="DateList" />
            <asp:TextBox runat="server" ID="CurrentTime" ></asp:TextBox>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Code" />
        </Triggers>
    </asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

背后的代码是这样的......

 protected void Code_TextChanged(object sender, EventArgs e)
    {
        //Adding current time (minutes and seconds) into dropdownlist
        DateList.Items.Insert(0, new ListItem(DateTime.Now.ToString("mm:ss")));

        //Setting current time (minutes and seconds) into textbox
        CurrentTime.Text = DateTime.Now.ToString("mm:ss");
    }
Run Code Online (Sandbox Code Playgroud)

我添加了其他文本框以查看更改操作,请删除文本框.

  • 只需使用此解决方案删除AutoPostBack ="true"即可. (4认同)