6

I'm creating a website app(ASP.NET, C#) and I just want to know the best way to display error/warning messages. Is it better to do it by MessageBox or thru a Label? Just need some suggestions.

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
Googler
  • 119
  • 1
  • 5
  • 5
    What kind of messages? Validation? page not found errors? Be more specific – Yurii Hohan Oct 28 '11 at 12:16
  • 3
    Not quite worth the 2 down-votes IMO, a perfectly valid question. – Mantorok Oct 28 '11 at 13:02
  • 2
    @Googler - Just FYI, many people have downvoted although the question is legit. Please be more specific about your question. And mark it as Answer if you get what you are looking for. :) – Win Oct 28 '11 at 14:39

9 Answers9

2

I would prefer to do it in this way:-

 this.RegisterClientScriptBlock(typeof(string), "key",  string.Format("alert('{0}');", ex.Message), true);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

For server side validation, you can write a custom control (which I did) like this to display message consistantly through out the site.

enter image description here

For client side validation, you can use validation summary.

<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List"
    ShowMessageBox="true" ShowSummary="false" />
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
Win
  • 61,100
  • 13
  • 102
  • 181
1

Man,

I like Bootstrap from twitter, that is a sleek, intuitive, and powerful front-end framework for faster and easier web development.

[]'s

Rodrigo Reis
  • 1,097
  • 12
  • 21
1

Please be clear on what you want to validate on. If you are validating a login page which contains the username/Password, then a label displaying 'Invalid username/Password' is well enough for a user to recognize easily. Have a color for the label where user could easily notify.

BharathNadadur
  • 557
  • 1
  • 6
  • 13
1
There are many ways you can display the error message.

1)Simple Message Box.You may need add System.Windows as namespace in application.

2)The same message box look and feel you can create using the below code,

this.RegisterClientScriptBlock(typeof(string), "key",  string.Format("alert('{0}');", ex.Message), true);

3)Using InBuilt Asp.net validation control like Required Field Validator,RangeValidator,Validatio Summary.

4)Place a Label control on each page,use it as when required to display error msg.

5)Define a validation class,create rules and error message commonly used across apllications.

6)Using Javascript prompt and alert also,you can display it.
MahaSwetha
  • 1,058
  • 1
  • 12
  • 21
1

You question is bit unclear for me.

Anyway if we talking about general validation:

You can use JavaScript or jQuery which allow you to do client side validation, which is widely in use these days.

OR you can use asp.net validation control like RequiredFieldValidator

Also, please read this article about Building better web forms: Validation best practices and Using the Enterprise Library Validation Application Block in ASP.NET

user3723637
  • 130
  • 8
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
1

Firstly, you should be validating everything server side, because all client side mechanisms can be circumvented.

The normal convention for displaying error messages is to pair the message with the field that contains the invalid information.

You can also add client-side validation using HTML5 attributes, or JavaScript - or you can combine the two, but the warnings should still be paired with the field (and styled however you like).

<label>First name<br>
<input type="text" name="firstname" required></label>
<span class="error">You must enter a first name</span>
Fenton
  • 241,084
  • 71
  • 387
  • 401
1

Honestly it depends on the way you want your application to work. If you want to real-time validate user input on for instance a subscription form, you'd definitely don't want javascript alerts when you leave a textbox. So in that case I'd prefer the inline way that Sohnee describes.

But if you want to show the failure message of a application-critical action, I'd go with a javascript alert, or if a postback event raises warnings ( in the code behind ) you could write these in a container (a div or so) which will be visible when the page is rendered.

-1

There are plenty of sites using google that give good overviews of this topic. here is one good example.

czuroski
  • 4,316
  • 9
  • 50
  • 86
  • czuroski : how does Exception Handling can be a good example for this question? – huMpty duMpty Oct 28 '11 at 12:28
  • I may have misunderstood the question. I assumed the question was looking for a way to handle exceptions and display them to a user if necessary. – czuroski Oct 28 '11 at 12:33