Friday, February 11, 2011

Capture a value in a custom control in a child page (inside a master page) using ASP.Net validators

Most of the times we want to achieve consistency between pages. Then mostly we create a master page and keep the web pages consistence. In these pages sometime we use our custom controls (like a custom calendar control).

If you want to check using a required field validator whether the calendar control has been filled before clicking on the save button, there is no easy way of getting this done. This is because of the master page the fields name is getting replaced.  However you can get this done using the following code snippet. You need to add this to the onload event of the web page.

//Getting the content place holder
ContentPlaceHolder content = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");

//Finding the textbox using the previously found content place holder
TextBox txt = (TextBox)content.FindControl("CalendarControl1TextBox");

//Assigning the required field validator
RequiredFieldValidator1.ControlToValidate = txt.ID;

No comments:

Post a Comment

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...