与文本框里面的UpdatePanel问题 - 不会造成OnTextChanged事件 [英] Problem with textbox inside updatepanel - not causing OnTextChanged event

查看:172
本文介绍了与文本框里面的UpdatePanel问题 - 不会造成OnTextChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:我有一个Ajax的UpdatePanel内的文本框。只要在文本框中我必须显示一条消息,用户类型(不同的消息依赖于用户输入的数据)。

I have the following situation: I have a textbox inside an ajax updatepanel. Wherever the user types in the textbox I must display a message (different message that depends on the user typed data).

     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
        <ContentTemplate>
            <asp:TextBox ID="txtMyTexbox" runat="server" Width="500px" OnTextChanged="txtMyTexbox_TextChanged" AutoPostBack="true"></asp:TextBox>
            <br />
            <asp:Label ID="lblMessage" runat="server" CssClass="errorMessage" Visible="false">Hello World</asp:Label>
         </ContentTemplate>
            <Triggers>
             <asp:AsyncPostBackTrigger ControlID="txtMyTexbox" />
            </Triggers>
      </asp:UpdatePanel>

在服务器端我写了下面的页面加载

In server side I have written the following at page load

ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(txtMyTexbox);

和这样的方法

protected void txtMyTexbox_TextChanged(object sender, EventArgs e)
    {           
            if (.....)
            {
                lblMessage.Visible = false;
            }
            else
            {
                lblMessage.Visible = true;
            }            
    }

我现在的问题是:当文本框中的用户类型不会引起OnTextChanged事件

My problem now is that: when the user types in the textbox it doesn't cause OnTextChanged event.

我缺少的东西?

推荐答案

我不知道,你的问题有什么用的UpdatePanel

I'm not sure that your problem has anything to do with the UpdatePanel.

其实,框TextChanged 事件不火的 的。它只会火的 的文本框后失去重点。出现这种情况,如果直接的AutoPostBack 设置为,或在下次回发发生。请参阅该文档的<一个href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.autopostback.aspx">AutoPostBack物业和<一href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textchanged.aspx">TextChanged事件。

In fact, the TextChanged event doesn't fire while typing. It will only fire after the textbox loses focus. This happens directly if AutoPostBack is set to True, or when the next postback occurs. Please see the docs for the AutoPostBack property and the TextChanged event.

AFAIK,你最好的选择可能是处理JavaScript中的keyup事件。

Afaik, your best bet is probably to handle the keyup event in javascript.

下面是一个简单的jQuery的例子:

Here's a simple jQuery example:

$(document).ready(function() {
    $(':text[id$=YourTextBox]').keyup(function() {
        if ($(this).val() === "your special value") {
            $('span[id$=YourLabel]').css('visibility', 'visible');
        }
        else {
            $('span[id$=YourLabel]').css('visibility', 'hidden');
        }
    });
});

这篇关于与文本框里面的UpdatePanel问题 - 不会造成OnTextChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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