如何使一个文本框回发上的KeyUp? [英] How do I make a Textbox Postback on KeyUp?

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

问题描述

我有一个改变一个下拉在OnTextChanged事件中的内容文本框。此事件似乎​​触发当文本框失去焦点。我如何做到这一点的关键preSS或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?

下面是我的code为例

Here is an example of my code

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

因此​​,在codebehind,我绑定在页面加载下拉。 C $ c_TextChanged事件中的$刚刚重新绑定下拉。我想这样的事情发生在每个键preSS而不是在文本框失去焦点。

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.

我继承了这个code最近这不是为我做这些的理想的方法,但时间限制prevent我从网络servicy方法重写这一点。

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事件相匹配的变事件的文本框试了,但是这第一个关键pressed才可以。

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.

推荐答案

这将解决您的问题。逻辑是一样的由Kyle提出的解决方案。

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的背后是这样的...

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天全站免登陆