Validate – Web User Control

I do not like to use the calendar from .NET, so I would like to have one Web User Control with 3 drop down boxes, day, month, year. [CODE DONE].

I want to be able to call this Control and initialize it with start year and end year, and with or without selected date.[CODE DONE].

This control will see if there is one valid date selected and return bool [CODE DONE].

Then in my web page I would like to able to see if that web user control is valid, in a way that I can use with the normal .NET validation (associate one required field), the problem is that I don’t know where to put this code and retrieve it to the validation control on the web page. [CODE NOT DONE].

How can I do this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There are two steps to integrating your custom server controls with the validation framework.

(1) Server side: you’ll need to add a ValidationPropertyAttribute to your class, so the validation framwework knows what to look at when validating:

[ValidationProperty("SelectedDate")]
public class MyDateControl : WebControl
{
    public DateTime? SelectedDate { get { ... } set { ... } }
}

(2) To hook up with client side validation, you have to make sure there’s an input tag associated with your control. One way of doing that is rendering an <input type=”hidden”> as the first child tag of your web control’s HTML. The validation framework will pick up on that. The remaining thing to do here, is to set this hidden field through JavaScript each time your one drop downs changes.

This way, you can tie in with the existing validation controls. If you want different way to validate, you should look at a CustomValidator.

Method 2

You want to use the CustomValidator control for this. See this tutorial that explains how to implement it with both a client-side and server-side version of the validation.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x