Reputation: 23
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".
Upvotes: 0
Views: 73
Reputation: 48989
You have your 2nd isnull test without a ! (not) missing.
However, text boxes never return null, so this would be better:
First, the markup:
Text4:
<asp:TextBox ID="TextBox4" runat="server"
AutoPostBack="true"
OnTextChanged="TextBox4_TextChanged">
</asp:TextBox>
<br />
Text5:
<asp:TextBox ID="TextBox5" runat="server"
AutoPostBack="true"
OnTextChanged="TextBox4_TextChanged"
></asp:TextBox>
<br />
<h3>Result box</h3>
Text6:<asp:TextBox ID="TextBox6" runat="server">
</asp:TextBox>
And code behind:
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
// both text4 and text 5 have ontextchange = this event
if (TextBox4.Text != "" & TextBox5.Text !="")
{
TextBox6.Text =
(
(
Convert.ToDouble(TextBox4.Text) +
Convert.ToDouble(TextBox5.Text)
) / 2
).ToString();
}
}
result:
Ok, so then say (for example) some code (say a button) sets the 2 values, then we would have this code:
First, our markup:
Text4:
<asp:TextBox ID="TextBox4" runat="server" >
</asp:TextBox>
<br />
Text5:
<asp:TextBox ID="TextBox5" runat="server">
</asp:TextBox>
<br />
<h3>Result box</h3>
Text6:<asp:TextBox ID="TextBox6" runat="server">
</asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server"
Text="set the 2 text boxes"
OnClick="Button1_Click" />
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:
void CalulatorCode()
{
TextBox6.Text =
(
(
Convert.ToDouble(TextBox4.Text) +
Convert.ToDouble(TextBox5.Text)
) / 2
).ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox4.Text = "123";
TextBox5.Text = "456";
CalulatorCode();
}
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.
Upvotes: 0
Reputation: 490
Division operations have a higher priority than sum operations. I would recommend to add an extra parenthesis:
( this>(Convert.ToInt32(TextBox5.Text) + Convert.ToInt32(TextBox4.Text) )<this / 2)
Anyhow, your method will update TextBox6
, whenever TextBox6
changes.
This will not work.
I recommend having a different method (e.g: private void DoSum()
) and call this from both TextBox4_TextChanged
and TextBox5_TextChanged
.
Upvotes: 1