Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using Visual Studio 2015, webform, C#
I've got a textbox which the user enters a numeric value, upon tabbing out it fires the following code


protected void txtAsset_TextChanged(object sender, EventArgs e)
    {
        lblSuccessful.Text = string.Empty;
        txtAsset.Focus();
        string input = txtAsset.Text;
        if (!Regex.IsMatch(input, @"^[0-9]\d*"))
        {
            lblSuccessful.CssClass = "ErrorMessage";
            lblSuccessful.Text = "You have input invalid criteria";
            txtAsset.Text = string.Empty;
            txtAsset.Focus();
        }
        else
        {
          Execute Retrieval of record
        }


It works perfectly however, when the user types another number in sometimes you can type into the textbox othertimes it locks up, you can't delete the value or type anything.

Auto postback is on.

Any assistance appreciated

What I have tried:

I've removed the validation and it still happens, is this a Microsoft Bug
Posted
Updated 24-Sep-17 21:36pm
Comments
Karthik_Mahalingam 21-Sep-17 9:50am    
try inside an update panel, you shall achieve the same in JavaScript as well, which will eliminate the unnecessary round trips.

Look at your code: every time the txtAsset TextBox Text property is changed, you get a TextChanged event fired. And what do you do inside the event? Change the Text property of the same TextBox. Which causes another event to be fired. Which changes the Text, which causes an event, which ... you get the picture. Take the change to the textbox out, and your problem will disappear.
 
Share this answer
 
Hi Many thanks for your reply, I don't follow you.

My event only fires upon tabbing out of the textbox, if the validation is successful it enters the code to retrieve the record from the database, have stripped out most of the code and removed the validation and it still causes issues.
 
Share this answer
 
Set a breakpoint at the start of the code inside the event and step through the code. You will see exactly what is happening.

If you are not familiar with the debugger, here is a video that will get you started quickly: Basic Debugging with Visual Studio 2010 - YouTube[^]

And here is how to fix the StackOverflow exception:
C#
private bool isChanging;

protected void txtAsset_TextChanged(object sender, EventArgs e)
{
    // are we in the middle of chaning the txtAsset.Text property?
    // Yes, don't process the change that the code is in the middle
    //      of changing
    if (isChanging) return;
    
    // No? Okay, set the falg and make the change...
    isChanging = true;

    lblSuccessful.Text = string.Empty;
    txtAsset.Focus();
    string input = txtAsset.Text;
    if (!Regex.IsMatch(input, @"^[0-9]\d*"))
    {
        lblSuccessful.CssClass = "ErrorMessage";
        lblSuccessful.Text = "You have input invalid criteria";
        
        // this next line fires the txtAsset.TextChanged event...
        txtAsset.Text = string.Empty;
        
        txtAsset.Focus();
    }
    else
    {
      Execute Retrieval of record
    }
    
    //We are all done...
    isChanging = false;
}
 
Share this answer
 
v2

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