For an exercise I have to compare if 2 textboxes contain the same text with a custom validator (doing the same thing as a comparevalidator does) BUT I cant use a compare validator for this. I have to use the custom validator for this.
It has to compare TextBox1 and TextBox2, if these don't contain the same value ( text) it have to give the error message: not the same value!
How can I solve this?
This is what I have so far:
Markup:
<asp:TextBox ID="TextBox2" runat="server" />
<asp:TextBox ID="TextBox3" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Not the same value !" ControlToValidate="TextBox2" onservervalidate="CustomValidator1_ServerValidate" />
And This is what I tried to do in code-behind:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (TextBox2.ToString() != TextBox3.ToString())
{
Label1.Text = "Not the same values";
// don't exactly know what to do in here in order to put it in the error message
}
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (TextBox2.Text != TextBox3.Text)
{
Label1.Text = "Not the same values";
args.IsValid = false;
}
}
Refer this Discussion .its about your Question and it has a solution
Updated:
Try this :
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Not the same value !" ControlToValidate="TextBox2" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (TextBox2.Text == TextBox3.Text)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.