protected void TextBox6_TextChanged(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(TextBox4.Text) && String.IsNullOrEmpty(TextBox5.Text))
{
TextBox6.Text = Convert.ToString(Convert.ToInt32(TextBox5.Text) + Convert.ToInt32(TextBox4.Text) / 2).To String();
}
}
TextBox6 Is Not giving me any Answer, AutopostBack is set to "True".
2
Answers
Division operations have a higher priority than sum operations.
I would recommend to add an extra parenthesis:
Anyhow, your method will update
TextBox6
, wheneverTextBox6
changes.This will not work.
I recommend having a different method (e.g: private void
DoSum()
) and call this from bothTextBox4_TextChanged
andTextBox5_TextChanged
.You have your 2nd isnull test without a ! (not) missing.
However, text boxes never return null, so this would be better:
First, the markup:
And code behind:
result:
Edit: Some other code set the 2 values in the text box
Ok, so then say (for example) some code (say a button) sets the 2 values, then we would have this code:
First, our markup:
so, I just dropped in a button, but "some how" and "some palce" those 2 textboxes will get a value set.
however, when you set a textbox (with code), then that those text box events don’t THEN fire for you. Those control events ONLY fire when the user changes them, NOT code behind.
So, if a bunch of code is going to update (or fill or set) some values on a page, and you need some calulations to occur?
Then build a "calulator" routine for the page that does all the math etc. for all the controls.
Thus our code becomes this:
So, if the controls are to have some values, then yes, YOU the developer will have to write/have/use code to call the calculation routines. just setting the values from code will not trigger any event code attached to each control.