Reputation: 5554
everybody; I have this problem in asp.net, I have a page where I insert and modify data, before saving I make a validation if it passes I save the data but if not I raise an exception and show it, the function goes 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 raise the exception and fix the data in the page I click again the save button and it saves but before it shows me again the exception message and its annoying. Even when the data is saved I click again and it shows the message from the exception again.
Do you know the answer for this issue?
Upvotes: 0
Views: 175
Reputation: 67068
If JSLiteral is a server side control and it's using view state. Then you'd need to clear the state of the control, when the save is succesful.
You could disable the viewstate for the control like JSLiteral.EnableViewState =false;
Upvotes: 1
Reputation: 103485
Throwing & catching an exception in the same function is basically pointless (exception are intended to go across call levels). Everything would probably be better if you wrote it as:
if(ValidData())
{
//Save
}
else
{ // Javascript alert
JSLiteral.Text = Utilities.JSAlert(ex.Message);
}
Upvotes: 0
Reputation: 1518
My initial guess is that ViewState remembers the error message. Try disabling ViewState on the JSLiteral control.
Upvotes: 0
Reputation: 17804
Are you resetting the value of JSLiteral to empty after you save?
Upvotes: 1
Reputation: 4011
Is the message saved in the viewstate of the literal?
Explicitly set the literal text to nothing if the data is valid.
Upvotes: 0