如何在 KeyUp 上进行文本框回发? [英] How do I make a Textbox Postback on KeyUp?

查看:24
本文介绍了如何在 KeyUp 上进行文本框回发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Textbox that changes the content of a dropdown in the OnTextChanged event. This event seems to fire when the textbox loses focus. How do I make this happen on the keypress or keyup event?

这是我的代码示例

<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>

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

So in the codebehind, I bind the dropdown on page load. The Code_TextChanged event just rebinds the dropdown. I would like this to happen on each keypress rather than when the textbox loses focus.

我最近继承了这段代码,这对我来说不是这样做的理想方法,但时间限制使我无法在网络服务方法中重写它.

I inherited this code recently and this is not the ideal method of doing this for me, but time limitations prevent me from rewriting this in a web servicy method.

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

I have tried using jQuery to bind the "keyup" event to match the "change" event for the textbox, but this only works on the first key pressed.

推荐答案

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

This will solve your problem. Logic is same as the solution suggested by Kyle.

看看这个.

<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>

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

Code behind goes like this...

 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");
    }

我添加了额外的文本框以查看操作中的变化,请删除文本框.

I have added additional textbox to see change in action, do remove the textbox.

这篇关于如何在 KeyUp 上进行文本框回发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆