I’m using ASP.NET to implement a site and need some help giving the user some feedback when submitting a form.
I use a detailsview to have the user insert the data needed but when I want to give the user some feedback when the insert succeeds or fails. So if the insert is successfull I want to run the JS function to show a success notification, and otherwise an error notification.
I tried using
OnInserted="areaInsertHandler"
On the SQL data source and then check for exceptions using:
protected void areaInsertHandler(Object sender, SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
// There was an error in submitting, show the error notification
ClientScript.RegisterClientScriptBlock(GetType(), "DialogHandler", "showError();", true);
e.ExceptionHandled = true;
}
else
{
// Show success
ClientScript.RegisterClientScriptBlock(GetType(), "DialogHandler", "showSuccess();", true);
}
}
But when I submit neither of the functions seem to fire. Using a browser debugger the JS functions are not even entered, so it seems that they aren’t called. Using the VS debugger I can see that the handler is indeed called and the ClientScript code is executed. So I’m not sure why nothing happens in the browser.
The detailsview is inside a updatepanel if that makes any difference.
Any feedback or other suggestions on how to approach the problem would be greatly appreciated.
Kind regards
Solved it. Leaving this answer here incase anyone else runs into similar troubles.
The problem is that I was using an updatepanel around my form, which means that it can’t add the script to the header as the page isn’t reloading in the usual sense. Instead it is using partial postbacks which only replaces parts of the body without reloading the site.
The solution is to register the script with the scriptmanager instead of clientscript, like so:
Easy =)