Question

I have a bunch of text boxes and a save button to update something. When I click Save I have code that determines whether they are correctly filled in, in the code behind file.

If they are not correctly filled in, I want to display an error message in the form of an alert.

What is the best way to do this? Pressing the button obviously makes the page postback, so I thought about adding something to the URL like mypage.aspx?errormessage=blahblah but I don't know if this is the best way or even how to do it...

Any suggestions?

Was it helpful?

Solution

Modal alerts are bad, as are postbacks. Try to check as much as possible on the client-side without a round-trip to server. See jQuery Validation plugins for a less intrusive way of validation.

OTHER TIPS

Could you use a CustomValidator to trigger client side script that shows a alert box?

You could use the ClientScript.RegisterStartupScript() method in the server side error handling code to write a javascript snippet which calls alert('message'), something like this

private void ShowErrorMessage(string message)
{
    string script = "alert('" + message + "');";
    ClientScript.RegisterStartupScript(typeof(MyPage), "errorScript", script, true);
}

But I would recommend you use a validator instead. If you implement your own custom validator, you can make it emit client-side script which can run before the submit, to avoid the postback altogether.

A good thing about validators is that their error messages can be displayed in a ValidatorSummary on the page, avoiding the ugly alert box.

First of all I won't recommend showing an modal alert box to the user.

You can call a javascript function from the server side code and in that function you can pop out the error.

Or you can issue an AJAX request and after the validation on server side you can send back a response to the client.

ASP.NET's various validation controls (with client-side validation enabled), coupled with proper error messages and/or summary message will be good enough for most scenarios.

For 'AJAX feel and behaviour', put the controls into an updatepanel will be easy to implement too.

Use ye olde Validators as much as poss, they render out some javascript to the client so yu can do alot of validation using these controls and only when they are all satisfied does it allow the page to submit.

They do fire on every submit so if you don't want every submit action to fire them you make the controls part of a validation group.

They can validate input using regular expressions, make sure a field has a value and there is a few more as well.

Then there is property to declare a 'message' and a control to show all the validator messages. All very swish and built right into the IDE.

Go check out the validator controls.

Try the following code in your button click event:

string strErr="Error!";//Put your error message here
ClientScript.RegisterStartupScript(GetType(), "scrptName", "javascript: alert('"+strErr+"'); ", true);

It will show an alert message.

Else put a label on your aspx page and set visible false in page_load event. When error occurs in your button event set the label visibility 'true' and fill the label text with the error message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top