views:

552

answers:

8

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?

A: 

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.

rahul
+3  A: 

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.

Anton Gogolev
Postback has the advantage of having to write the validation code only once. And change it only once. I've seen quite a few web apps where server-side validation had different results than client-side ~.
Joey
True, but you can always issue an AJAX request from JS validation code. This won't be a postback from users' point of view.
Anton Gogolev
Ah, sorry then. I somehow included AJAX requests in "postback". My bad.
Joey
+1  A: 

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

Thomas Sandberg
A: 

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.

o.k.w
A: 

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.

Robert
+1  A: 

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.

Tor Haugen
A: 

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

you can use this simple way

protected void btnSave_Click(object sender, EventArgs e)
{
    //check whether they are correctly filled in
    if (!theyarenotcorrectlyfiledIN)
    {
        lblMsg.Text = "your message";
        return;
    }
}

where lblMsg is label on the top the textbox fields

<asp:Label runat="server" ID="lblMsg"></asp:Label>
Muhammad Akhtar
A: 

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.

Himadri