简体   繁体   English

未触发文字更改事件

[英]text changed event not fired

I need to add 1 event for textbox into my webpage (created in ASP.NET with C#) and I was declared in the Page_Load function and into asp syntax: 我需要将文本框的1个事件添加到我的网页中(使用C#在ASP.NET中创建),并在Page_Load函数和asp语法中声明了我:

 protected void Page_Load(object sender, EventArgs e)
    {
        textbox1.TextChanged += new EventHandler(textbox1_TextChanged);
    }

public void textbox1_TextChanged(object sender, EventArgs e)
{
    if (textbox1.Text == "ABCD")
    {
        Image1.Visible = true;
        textbox1.Enabled = false;
    }
}

and into asp page i used this statement: 并进入asp页面,我使用了以下语句:

<asp:TextBox Width="200" ID="textbox1" runat="server"></asp:TextBox>

I did debug and found that not execute textbox1_TextChanged function 我做了调试,发现没有执行textbox1_TextChanged函数

Why ? 为什么呢

you need to set AutoPostBack to true . 您需要将AutoPostBack设置为true
see msdn for this: 参见msdn

To have the TextChanged event cause an immediate posting, set the TextBox control's AutoPostBack property to true. 若要使TextChanged事件引起立即发布,请将TextBox控件的AutoPostBack属性设置为true。

Well, better late than never: you declared a method, strictly speaking, a handler for the event. 好了,迟到总比没有好:严格地说,您声明了该事件的处理程序 But you didn't bind the event to the handler, like this: 但是您没有将事件绑定到处理程序,如下所示:

<asp:TextBox Width="200" ID="textbox1" OnTextChanged="textbox1_TextChanged" runat="server"></asp:TextBox>

What you missed: OnTextChanged="textbox1_TextChanged" 您错过了什么: OnTextChanged="textbox1_TextChanged"

In other words, your method would never be called because you never told the control that method was a handler for the event. 换句话说,永远不会调用您的方法,因为您从未告诉控件该方法是事件的处理程序。

我认为值得一提的是,如果文本值实际未更改,则不会触发TextChanged事件,即您设置了文本,但将其设置为与以前相同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM