Display Validator error message in span control if validation fails

Posted by Shivanichhabra4u under ASP.NET on 2/24/2009 | Views : 3790 | Status : [Member] | Replies : 2

I am using one required field validator to validate a text box.

It displays error message when validation fails.

But i do not want to display error message where the validator is placed. I want to display that message somewhere else where my one label control is placed.
It means whenever validation fails, error message should display in label. I used below method, but it continuously display error message in label. It should display only when error comes

label1.text = Requiredfieildvalidator1.errormessage

Is it possible...

Please help me out


Posted by: Neeks on: 2/25/2009 [Member] Bronze

Hi Shivani,
As you have mentioned, you want to display the error message at the place where you want. It is possible.
You cannot execute it by assigning the value to the Label text.
Because both are server control.
You cannot assign the property in that way.

You can use the control validationsummary.

For that control you have to do following coding

<form id="form1" runat="server">

FName<asp:TextBox runat="server" ID="txt">
</asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="FName" Text="*" ControlToValidate="txt"></asp:RequiredFieldValidator>
<br />
LName<asp:TextBox runat="server" ID="TextBox1">
</asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="LName" Text="*" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
<asp:validationsummary ID="Validationsummary1" runat="server"></asp:validationsummary>
<asp:Button runat="server" ID="btn" Text="Save" />

Shivanichhabra4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 2/25/2009 [Member] [MVP] [Administrator] NotApplicable

Good Afternoon Shivanichhabra4u

Please stop Posting the same Subject with a Different Question.

Thank you for posting at Dotnetfunda

Shivanichhabra4u, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response