Reputation: 3912
How to show an error messages
or data validation messages
in Asp.net with c# language.
Please guide me.
Upvotes: 1
Views: 5123
Reputation: 23142
There are lots of ways to accomplish this, depending your particular situation, but you might try using one of the ASP.NET validation controls, like RequiredFieldValidator or RegularExpressionValidator.
Example
<asp:TextBox id="textbox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqFldValidator"
runat="server"
ControlToValidate="textbox1"
ErrorMessage="Field is required!" />
<asp:RegularExpressionValidator ID="regExValidator"
runat="server"
ControlToValidate="textbox1"
ValidationExpression="\d+"
ErrorMessage="Must be a number!"/>
The preceding ASP.NET markup is all you need to perform a couple of basic validations on a TextBox. If the the form is submitted and the TextBox is blank, the RequiredFieldValidator will preempt the postback and show the message "Field is required!". Similarly, if the text in the TextBox does not match the ValidationExpression of the RegularExpressionValidator, the message "Must be a number!" will be shown.
You can do a lot more with the ASP.NET validation controls, but this is just a basic example. To learn more, consult the MSDN documentation.
Upvotes: 0
Reputation: 172
Use te validation summary along with the asp.net validators. Check this msdn documentation.At the end of the webpage you'll find a nice and easy example how how to use validation summaries. Happy coding
Upvotes: 0
Reputation: 78487
Markup:
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(
this.GetType(), "myalert", "alert('" + errorText + "');", true);
}
or
Response.Write(
@"<SCRIPT LANGUAGE=""JavaScript"">alert('" + errorText + "')</SCRIPT>");
Upvotes: 1