Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Usercontrol and validation 1

Status
Not open for further replies.

AndyH1

Programmer
Jan 11, 2004
350
GB
I have a user control in some code Ive inherited which allows the user to select a day, month, year from three dropdowns

<%@ Control language="C#" AutoEventWireup="true" inherits="Website.dateDropdown" Codebehind="dateDropdown.ascx.cs" %>
<asp:DropDownList id="day" runat="server">
</asp:DropDownList>
<asp:DropDownList id="month" runat="server">
</asp:DropDownList>
<asp:DropDownList id="year" runat="server">
</asp:DropDownList>

I then populate each dropdown with a blank (meaning nothing selected) followed by the appropriate values (ie day 1 to 31, month jan ... december, 1990 to today)

This all works fine, but there is a requirement for it to be mandatory and for a valid date.

I can add a validator for each field in the control, ie

<asp:RequiredFieldValidator id="RequiredFieldValidator1"
runat="server"
ErrorMessage="Enter a day "
ControlToValidate="day"
InitialValue=" ">
</asp:RequiredFieldValidator>
...same for month...same for year

it works, but thats not really what I'm after. Really I just want one error to be displayed if one or more of the fields (day, month, year) are left blank ie 'You must enter a valid date'.
The other problem is a really need this message to be displayed external to the user control, if thats possible as most of the other validations are in the form
<td>field</td><td>Validation message</td>

ie I really want a valdaitor message on a whole uc itself or some way of passing the message out of the user control.

If anyone has any ideas I'd be grateful

Thanks
AndyH1



, but I cannot think how to do this. can anyone suggest a solution

Thanks
AndyH1
 
Use a custom validator to check if each field is filled in.
As for where the validation occurs, it should be on the usercontron since that is where the input fields are. User controls are supposed to be reusable, it you tie it to the page, then there is really no need for a usercontrol because you take the reusability away.
 
Thanks jmeckley and jbenson001,

If the code was my own I'd probably do as you suggested and use a true date picking control, but the customer is insistent on the look remaining the same.

The previous programmer did add a custom control in the user control(see below) but for some reason it doesn't seem to work and I cannot figure out why, since a date that consists of " "," "," " would have given the exception

<asp:CustomValidator runat="server" Display="Dynamic" ControlToValidate="year" ErrorMessage="Please enter a proper date" OnServerValidate="ChecktheDate" />


protected void ChecktheDate(object sender, ServerValidateEventArgs e)
{
try
{
DateTime d = new DateTime(Int32.Parse(year.SelectedValue), Int32.Parse(month.SelectedValue), Int32.Parse(day.SelectedValue));
e.IsValid = true;
}
catch (Exception)
{
e.IsValid = false;
}
}


}
}

thanks both
AndyH1
 
The code should work, but you will have to trace through and find out the problem.
 
Thanks jbenson

have tried just using the code

protected void CheckDate(object sender, ServerValidateEventArgs e)
{
e.IsValid = false;
}

and this isn't being activated. Removing protected (or replacing it with public) just results in an error that it can't find the routine CheckDate so it doesn't look like its the logic in the routine itself.
 
Manged to sort - it was the logic
AndyH1
 
Sorry didn't manage to sort. I now have

<%@ Control language="C#" AutoEventWireup="true" inherits="Website.dateDropdown" Codebehind="dateDropdown.ascx.cs" %>

<asp:DropDownList id="day" runat="server">
</asp:DropDownList>
<asp:DropDownList id="month" runat="server">
</asp:DropDownList>
<asp:DropDownList id="year" runat="server" CausesValidation="True">
</asp:DropDownList>
<asp:CustomValidator runat="server" Display="Dynamic" ControlToValidate="year" ErrorMessage="Please enter a valid date" OnServerValidate="ChecktheDate" />

and set the dropdowns to empty rather than " ".

and have the code to validate:

protected void ChecktheDate(object sender, ServerValidateEventArgs e)
{

if (String.IsNullOrEmpty(day.SelectedValue) || String.IsNullOrEmpty(month.SelectedValue) || String.IsNullOrEmpty(year.SelectedValue ))
e.IsValid = false;
else e.IsValid = true;
}

If I leave the day blank it picks this up, if I make the month blank it picks it up, but if I make the year blank it doesn't pick it up. If I have any combination which includes the year being blank it doesn't pick it up.

It seems to be related to the ControlToValidate in the customvalidator. If I set this to month I get the same problem only with the month.

I guess there must be some way of getting the value of the dropdown to which ControlToValidate being set from the ServerValidateEventArgs e arguments, but cannot see how I get the selected value from this.

Any ideas please...
Thanks
AndyH1

 
Sorry didn't manage to sort. I now have

<%@ Control language="C#" AutoEventWireup="true" inherits="Website.dateDropdown" Codebehind="dateDropdown.ascx.cs" %>

<asp:DropDownList id="day" runat="server">
</asp:DropDownList>
<asp:DropDownList id="month" runat="server">
</asp:DropDownList>
<asp:DropDownList id="year" runat="server" CausesValidation="True">
</asp:DropDownList>
<asp:CustomValidator runat="server" Display="Dynamic" ControlToValidate="year" ErrorMessage="Please enter a valid date" OnServerValidate="ChecktheDate" />

and set the dropdowns to empty rather than " ".

and have the code to validate:

protected void ChecktheDate(object sender, ServerValidateEventArgs e)
{

if (String.IsNullOrEmpty(day.SelectedValue) || String.IsNullOrEmpty(month.SelectedValue) || String.IsNullOrEmpty(year.SelectedValue ))
e.IsValid = false;
else e.IsValid = true;
}

If I leave the day blank it picks this up, if I make the month blank it picks it up, but if I make the year blank it doesn't pick it up. If I have any combination which includes the year being blank it doesn't pick it up.

It seems to be related to the ControlToValidate in the customvalidator. If I set this to month I get the same problem only with the month.

I guess there must be some way of getting the value of the dropdown to which ControlToValidate being set from the ServerValidateEventArgs e arguments, but cannot see how I get the selected value from this.

Any ideas please...
Thanks
AndyH1
 
Sorry didn't manage to sort. I now have

<%@ Control language="C#" AutoEventWireup="true" inherits="Website.dateDropdown" Codebehind="dateDropdown.ascx.cs" %>

<asp:DropDownList id="day" runat="server">
</asp:DropDownList>
<asp:DropDownList id="month" runat="server">
</asp:DropDownList>
<asp:DropDownList id="year" runat="server" CausesValidation="True">
</asp:DropDownList>
<asp:CustomValidator runat="server" Display="Dynamic" ControlToValidate="year" ErrorMessage="Please enter a valid date" OnServerValidate="ChecktheDate" />

and set the dropdowns to empty rather than " ".

and have the code to validate:

protected void ChecktheDate(object sender, ServerValidateEventArgs e)
{

if (String.IsNullOrEmpty(day.SelectedValue) || String.IsNullOrEmpty(month.SelectedValue) || String.IsNullOrEmpty(year.SelectedValue ))
e.IsValid = false;
else e.IsValid = true;
}

If I leave the day blank it picks this up, if I make the month blank it picks it up, but if I make the year blank it doesn't pick it up. If I have any combination which includes the year being blank it doesn't pick it up.

It seems to be related to the ControlToValidate in the customvalidator. If I set this to month I get the same problem only with the month.

I guess there must be some way of getting the value of the dropdown to which ControlToValidate being set from the ServerValidateEventArgs e arguments, but cannot see how I get the selected value from this.

Any ideas please...
Thanks
AndyH1
 
don't set the control to validate. also you are not validating the date is valid, only that the user put a value in.

what happens if the user selects
month day year
1 30 2009
this should fail, not roll to February 2, 2009.

all this is mute if you move them to an actual date picker UI. I would encourage you to discuss this option with the client, rather than continued down this path.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top