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:
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.
我认为值得一提的是,如果文本值实际未更改,则不会触发TextChanged事件,即您设置了文本,但将其设置为与以前相同的值。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.