compare validator to validate date in user control

S

Shailesh Katti

Hi,

I have created a user control using calender control and text box
control. When user selects value from calender control, it is shown in
text box control. user can directly enter the date in textbox control.

I have five such user controls on the web form. I tried using compare
validator to compare the dates. It doesn't understand the user control's
ID.

Is there a way to do it. Also to achieve this is there any better way to
do it ?

Pls. help !!!

Thanks.

Shailesh
 
T

Teemu Keiski

Couldn't you put the validator inside the user control as TextBox and
Calendar? Sounds reasonable to me as TextBox really is inside the user
control as well and then you wouldn't need to multiply the validation
because of multiple user controls. Inside the user control you specify the
control id for the validator as you would on normal page, that is the ID of
the control to be validated.

However, if you want to separate the validation, you would perhaps expose
TextBox's Text property as new top-level property in the user control and
apply ValidationProperty attribute for it. That way validation framework
would recognize the ID (of the user control) you provide for the validation
control.

Example of the second approach:

**ASCX of the user control:

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="DemoUserControl.ascx.cs"
Inherits="ControlDemoApp.DemoUserControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:Calendar ID="Calendar1" Runat="server" /><br>
<asp:TextBox ID="TextBox1" Runat="server" />

**Code-behind of the user control (the relevant part

[ValidationProperty("CalendarText")]
public class DemoUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Calendar Calendar1;
protected System.Web.UI.WebControls.TextBox TextBox1;

public string CalendarText
{
get
{
return TextBox1.Text;
}
set
{
TextBox1.Text=value;
}
}
....
}

Then usage on the ASPX Page:
<%@ Page language="c#" Codebehind="login.aspx.cs" AutoEventWireup="false"
Inherits="ControlDemoApp.login" %>
<%@ Register TagPrefix="uc1" TagName="DemoUserControl"
Src="DemoUserControl.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>

</HEAD>
<body >

<form id="Form1" method="post" runat="server">

<uc1:DemoUserControl id="DemoUserControl1"
runat="server"></uc1:DemoUserControl>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ErrorMessage="Calendar Date is required"
ControlToValidate="DemoUserControl1"></asp:RequiredFieldValidator>
<asp:CompareValidator id="CompareValidator1" runat="server"
ErrorMessage="Insert a valid date!" ControlToValidate="DemoUserControl1"
Operator="DataTypeCheck" Type="Date"></asp:CompareValidator>
<asp:Button id="Button1" runat="server" Text="Test it"></asp:Button>

</form>

</body>
</HTML>

--
Teemu Keiski
MCP, Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top