Simple display with OnChange event on textbox
35,919
To use TextChangedEvent
you need to add TextChangedEvent
event handler in your code and set AutoPostBack=true
in markup
<asp:TextBox ID="TextBox2" runat="server" OnTextChanged="TextBox2_TextChanged" AutoPostBack="true"></asp:TextBox>
Code behind
protected void TextBox2_TextChanged(object sender,EventArgs e)
{
int num1;
if(!Int32.TryParse(TextBox1.Text, out num1))
{
Label1.Text = "Not a valid number";
return;
}
int num2;
if(!Int32.TryParse(TextBox2.Text, out num2))
{
Label1.Text = "Not a valid number";
return;
}
sum = num1 + num2;
Label1.Text = sum.ToString();
}
Related videos on Youtube
Author by
CoderBeginner
Updated on May 27, 2020Comments
-
CoderBeginner almost 4 years
How to use the OnChange event to display the result on the Label1 instantly after value have enter in Textbox1 and Textbox2??
<div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> --------------------------------------------------------------------------------------- //Code behind int num1; if(!Int32.TryParse(TextBox1.Text, out num1)) { Label1.Text = "Not a valid number"; return; } int num2; if(!Int32.TryParse(TextBox2.Text, out num2)) { Label1.Text = "Not a valid number"; return; } sum = num1 + num2; Label1.Text = sum.ToString();
-
Cody Gray almost 11 yearsI don't see any event handler functions in your sample code... Is that what you're asking about, how to wire them up?
-
-
Eternal over 6 yearsthanks, had the same problem in a search box didnt knw had to unable autopostback