开发者

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 i开发者_Python百科n the Page_Load function and into asp syntax:

 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:TextBox Width="200" ID="textbox1" runat="server"></asp:TextBox>

I did debug and found that not execute textbox1_TextChanged function

Why ?


you need to set AutoPostBack to true.
see msdn for this:

To have the TextChanged event cause an immediate posting, set the TextBox control's AutoPostBack property to 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"

In other words, your method would never be called because you never told the control that method was a handler for the event.


I think its also worth noting that the TextChanged event does not fire if the text value did not actually change, i.e. you set the text but you set it to the same value it had previously.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜