Display Validator error message in span control if validation fails

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

I want to display required field validator's error message in span/label control if validation fails only.

Plesae help me out........




Responses

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

Up
0
Down
Good Day Shivanichhabra4u

Carry the Value in the string Variable and reassign it to the Label.

Thank you for posting at Dotnetfunda
[Administrator]

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

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

Up
0
Down
Hi Shivani,
I would be better if you explain the problem in details. I am not getting you.



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

Posted by: Ashish on: 2/25/2009 [Member] Starter

Up
0
Down
Hi Shivani ,
set EnableClientScript=false for RequiredValidator
Retrive RequiredValidator error message in lable
Label1.text=RequiredValidator1.errormessage.tostring()
Example:--
1 button , 2 textbox , 1 label , 2 requiredfieldvalidation(set proeperty EnableClientScript=false )
---------------------------------------
HTML CODE

<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label" Width="533px" BackColor="#FFE0C0" ></asp:Label>
<table>
<tr>
<td style="width: 143px">
</td>
<td style="width: 100px">
</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 143px">
Employee Name</td>
<td style="width: 100px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Enter employee name" Width="138px" EnableClientScript="False"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 143px">
Age</td>
<td style="width: 100px">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
ErrorMessage="Enter Age" EnableClientScript="False"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 143px">
</td>
<td style="width: 100px">
<asp:Button ID="Button1" runat="server" Text="Submit" /></td>
<td style="width: 100px">
</td>
</tr>

</table>

</div>
</form>
----------------------------------------------------------------------------
CODE BEHIND PART:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If IsValid = False Then
Label1.Text = RequiredFieldValidator1.ErrorMessage.ToString()
End If
end sub
--------------------------------------------------------------------------------
Hope it will Help You

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

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

Up
0
Down
Hi Ashish,
We can achieve the goal using the code provided by you,
But there is a big problem in the code is that it allows to postback the page. which is not good for the validation.
This is the server side validation.

This Page get posted even if there is an error in the page data entry. Which should be stopped.

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

Posted by: Ashish on: 2/26/2009 [Member] Starter

Up
0
Down
Hi Neeks ,

Yes,I agree with you . That my code allows server side validation .

But as per Shivani's requirement it is not possible to validate clientside.Because for saving requiredfieldvalidator value in lable , page must be visited to server side.

If any other way for that then kindly convey me.I will also try for same.

Thanks for you comment.

Warm Regards
Ashish Tiwari
'Rome was not built in a day '




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

Login to post response