文本更改事件未触发

Aly*_*zzu 1 c# asp.net events

我需要在我的网页中添加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;
    }
}
Run Code Online (Sandbox Code Playgroud)

进入asp页面我使用了这个声明:

<asp:TextBox Width="200" ID="textbox1" runat="server"></asp:TextBox>
Run Code Online (Sandbox Code Playgroud)

我做了调试,发现没有执行textbox1_TextChanged函数

为什么?

And*_*air 10

你需要设置AutoPostBacktrue.
请看msdn:

要使TextChanged事件立即发布,请将TextBox控件的AutoPostBack属性设置为true.

  • 但请记住:"只有当用户离开控件时,每次用户输入击键时,TextBox Web服务器控件都不会引发事件." (2认同)