Annoying exception in asp.net

all; I have this problem in asp.net, I have a page where I insert and modify data, before saving I do a check if it passes. I save the data, but if I don't throw an exception and show it, the function looks like this:

protected void btnSave_Click(object sender, EventArgs e)
{
try
{
...
if(ValidData())
  //Save
  ...
else
  throw new Exception("Invalid data");
}
catch(Exception ex)
{
  // Javascript alert
  JSLiteral.Text = Utilities.JSAlert(ex.Message);
}
}

      

The problem is that after I throw an exception and fix the data on the page, I hit the save button again and save it, but before it shows me the exception message again and is annoying. Even when the data is saved, I click again and again throws the message from the exception.

Do you know the answer to this question?

0


source to share


6 answers


Where JSLiteral is a server side control and uses view state. Then you need to clear the state of the control when the save is successful.



You can disable the viewstate for the control, like JSLiteral.EnableViewState = false;

+1


source


Will you reset the JSLiteral value to empty after saving?



+1


source


Disable the viewstate for your JSLiteral control.

+1


source


Is the message stored in the literal state view?

I will not explicitly provide literal text if the data is valid.

0


source


My initial guess is that the ViewState remembers the error message. Try disabling ViewState in your JSLiteral control.

0


source


Throwing and catching an exception in the same function is mostly pointless (the exception is meant to traverse call levels). Everything would be better if you wrote it like:

if(ValidData())
{
  //Save
}
 else
{  // Javascript alert
  JSLiteral.Text = Utilities.JSAlert(ex.Message);
}

      

0


source







All Articles