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;


Popular posts from this blog

Error message “User does not have send-as privilege.” when sending emails programmatically in CRM 2011