Click here to Skip to main content
15,908,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a web form that has a couple for textboxes on it. Right now I have a zero in these textboxes.

ASP.NET
<asp:TextBox ID="TextBoxTNUGSC" runat="server" Width="180px" 
                    style="text-align: right" AutoPostBack="True" 
                    ontextchanged="TextBoxTNUGSC_TextChanged">0</asp:TextBox>


Here is the textbox text change:
C#
protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBoxTHUG.Text.Replace(",", ""));
        int j = 12;
        TextBoxTHUGDR.Text = Convert.ToString(i / j);

        int a = Convert.ToInt32(TextBoxFTUG.Text.Replace(",", ""));
        int b = Convert.ToInt32(TextBoxFTG.Text.Replace(",", ""));
        int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));
        int d = Convert.ToInt32(TextBoxTHGDR.Text.Replace(",", ""));
        TextBoxT1234.Text = Convert.ToString(a + b + c + d);

        int g = Convert.ToInt32(TextBoxT1234.Text.Replace(",", ""));
        int f = Convert.ToInt32(TextBoxNCCDR.Text.Replace(",", ""));
        TextBoxTCNC.Text = Convert.ToString(g + f);

When a user comes into the web form they will see the zeros there in the textboxes. In order for the textbox textchange to work they will have to enter another 0 that will now display 00 in the textbox. How can I get the textbox textchange to fire if the user has nothing to enter into those textboxes?
Posted
Updated 11-Nov-14 7:33am
v3
Comments
ZurdoDev 11-Nov-14 9:55am    
Either start with the textboxes as empty and let them enter 0 or figure a different way to post back the data, perhaps allowing them to fill in all textboxes and then clicking a button.
Computer Wiz99 11-Nov-14 9:58am    
There is a problem with that. When the web form loads there are validators that load with it. Errors will show up before the form even loads completely.
ZurdoDev 11-Nov-14 10:01am    
I'm not sure what validators you are using but a validator shouldn't show until the form tries to post back.
Computer Wiz99 11-Nov-14 10:09am    
Range Validators. What I want to do and tring to do is to show the zeros when the web form loads but disappear when the user enters a value. Is there a way to do this in C#?
ZurdoDev 11-Nov-14 10:10am    
You may want to look into watermark for textboxes.

The clue is in the event name: "TextChanged"
It is intended to fire only when the text changes, and there is no good way to make it fire under other circumstances.

Instead, consider handling the Leave event and doing your processing there. You get Leave when the user tries to move out of the control - which should work whether the user changed anything or not.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 11-Nov-14 14:44pm    
Just note that while TextChanged event is handled on the server side (in OP's case at least) the 'Leave' event is exists only on the client side, at the JavaScript level - it called blur!!!
Based on the code, provided wouldn't you initialise the fields when it is first loaded by overriding the Page Load function? Is this what you were looking for?

C#
void OnLoad()
{
    updateText(0, 0, 0);
}

void updateText(int i, int abcd, int gf)
{
    int j = 12;
    TextBoxTHUGDR.Text = Convert.ToString(i / j);
    TextBoxT1234.Text = Convert.ToString(abcd);
    TextBoxTCNC.Text = Convert.ToString(gf);
}


protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
{
        int i = Convert.ToInt32(TextBoxTHUG.Text.Replace(",", ""));

        int a = Convert.ToInt32(TextBoxFTUG.Text.Replace(",", ""));
        int b = Convert.ToInt32(TextBoxFTG.Text.Replace(",", ""));
        int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));
        int d = Convert.ToInt32(TextBoxTHGDR.Text.Replace(",", ""));

        int g = Convert.ToInt32(TextBoxT1234.Text.Replace(",", ""));
        int f = Convert.ToInt32(TextBoxNCCDR.Text.Replace(",", ""));

        updateText(i, a+b+c+d, g+f);

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900