Answered by:
Usercontrol validation not working - code posted

Question
-
User-1357500346 posted
When I click the submit button the validation on one of the textboxes should kick in. Since I set the enabled property to true on the requiredfieldvalidator.
ascx page
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TextUserControl.ascx.cs" Inherits="TextUserControl" %>
<asp:TextBox ID="TextBox1" runat="server" CausesValidation="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1"
runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" ShowSummary="false" />code behind
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class TextUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public bool DateRequired
{
get { return this.RequiredFieldValidator1.Enabled; }
set { this.RequiredFieldValidator1.Enabled = value; }
}
public string DateValidationGroup
{
get { return this.TextBox1.ValidationGroup; }
set
{
this.TextBox1.ValidationGroup = value;
this.ValidationSummary1.ValidationGroup = value;
if (DateRequired == true)
this.RequiredFieldValidator1.ValidationGroup = value;
}
}
}
aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TelerikDatePage.aspx.cs" Inherits="TelerikDatePage" %>
<%@ Register src="TextUserControl.ascx" tagname="TextUserControl" tagprefix="uc2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="true"
onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br />
<br />
<br />
<uc2:TextUserControl ID="TextUserControl1" runat="server" DateRequired="true" DateValidationGroup="1" />
<uc2:TextUserControl ID="TextUserControl2" runat="server" DateRequired="false" DateValidationGroup="2" />
</form>
</body>
</html>code behind
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = TelerikDate1.DateSelectedText;
}
Friday, March 28, 2008 8:48 PM
Answers
-
User-1357500346 posted
I figured it out. I had to put the validationsummary outside the usercontrol. It was repeating. Stupid mistake.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 29, 2008 1:59 PM
All replies
-
User-2116278700 posted
Hi,
Try to assign Validation Group Name to both the Validator and the Button on which you want to check the validation.
Saturday, March 29, 2008 2:43 AM -
User-1357500346 posted
I figured it out. I had to put the validationsummary outside the usercontrol. It was repeating. Stupid mistake.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 29, 2008 1:59 PM