\");","if (rbMale.Checked) { Response.Write(\"\"); }","else if (rbFemale.Checked) { Response.Write(\"\"); }","Response.Write(\"\");","Response.Write(\"\");","Response.Write(\"\");"," 3
\fCode under Reset Form Button:
txtName.Text = txtEmail.Text = txtMobile.Text = \"\"; rbMale.Checked = rbFemale.Checked = false; ucDate.Reset();
txtName.Focus();
Registering the User Control Application Level:
In this approach we register the User Control under the project in Web.config file and the advantage with
this is we can consume the Control in all the Web Forms of the project with out registering it again and again.","Note: To register a User Control in application level we have a restriction i.e. the UserControl and WebForm can’t
be present in the same folder, so add a new Folder under the project naming it as “UserControls” and drag the
“CalendarUserControl.ascx” into the “UserControls” folder so that the WebForm is in the root folder of our project
and UserControl is under “UserControls” folder which is present under the root folder.
To test the above first delete the “Register Directive” we have added in “TestCalendarUserControl.aspx”,
then open the Web.config file and write the following code inside tag:





Developing a Custom Control:
Open a new empty Asp.Net Web Application project naming it as “AspControls”, open the “Add New
Item” windows, select “Web Forms” in LHS and now on the RHS we find “Web Forms Server Control” select it,
name it as “CustomCalendar.cs” and click on “Add” button. This will define a class with the name
“CustomCalendar” inheriting for “WebControl” class of “System.Web.UI.WebControls” namespace and we need to
write all the code for designing and executing in this class only. Now if we look into the code we will find 2
attributes on top of the class “DefaultProperty” and “ToolBoxData”.
 DefaultProperty specifies the default property for this control and it’s “Text”
 ToolBoxData property is to specify the information about the code that should be generated when we
drag and drop the control from “ToolBox” on to a WebForm and right now the data will be as following:
[ToolboxData(\"<{0}:CustomCalendar runat=server>\")]"," In place of “{0}” the TagPrefix what we provide while registering the control will come and
“CustomCalendar” in the name of Control which is nothing but our class name, which we can either leave the same
or change it as per our requirements. Now delete all the existing code from the class and write our code inside the
class which should finally look as following:","public class CustomCalendar : WebControl {"," TextBox txtDate; ImageButton imgDate; Calendar cldDate;"," protected override void CreateChildControls() {"," txtDate = new TextBox(); txtDate.ID = \"txtDate\"; txtDate.Width = Unit.Pixel(200);"," imgDate = new ImageButton(); imgDate.ID = \"imgDate\"; imgDate.Click += ImgDate_Click;"," cldDate = new Calendar(); cldDate.ID = \"cldDate\"; cldDate.Visible = false; cldDate.NextMonthText = \"\";"," cldDate.FirstDayOfWeek = FirstDayOfWeek.Monday; cldDate.SelectionChanged += CldDate_SelectionChanged;"," cldDate.DayRender += CldDate_DayRender; cldDate.VisibleMonthChanged += CldDate_VisibleMonthChanged;"," this.Controls.Add(txtDate); this.Controls.Add(imgDate); this.Controls.Add(cldDate);"," }"," 4
\f private void ImgDate_Click(object sender, ImageClickEventArgs e) {
if (cldDate.Visible) { cldDate.Visible = false; }
else { cldDate.Visible = true; }
}
private void CldDate_SelectionChanged(object sender, EventArgs e) {
txtDate.Text = cldDate.SelectedDate.ToShortDateString(); cldDate.Visible = false;
}
private void CldDate_DayRender(object sender, DayRenderEventArgs e) {
if ((e.Day.Date.DayOfYear > DateTime.Now.DayOfYear && e.Day.Date.Year == DateTime.Now.Year)
|| e.Day.Date.Year > DateTime.Now.Year) {
e.Cell.ForeColor = System.Drawing.Color.Gray;
e.Day.IsSelectable = false; e.Cell.ToolTip = \"Out Of Range\";
}
}
private void CldDate_VisibleMonthChanged(object sender, MonthChangedEventArgs e) {
if (e.NewDate.Month == DateTime.Now.Month && e.NewDate.Year == DateTime.Now.Year) {
cldDate.NextMonthText = \"\";
}
else { cldDate.NextMonthText = \">\"; }
}
public DateTime SelectedDate {
get { EnsureChildControls(); return cldDate.SelectedDate; }
set { EnsureChildControls(); cldDate.SelectedDate = value; }
}
public string ButtonImageUrl {
get { EnsureChildControls(); return imgDate.ImageUrl; }
set { EnsureChildControls(); imgDate.ImageUrl = value; }
}
public void Reset() {
txtDate.Text = \"\"; cldDate.SelectedDate = DateTime.Today; cldDate.VisibleDate = DateTime.Now;
}
public override void RenderControl(HtmlTextWriter writer) {
txtDate.RenderControl(writer); imgDate.RenderControl(writer); cldDate.RenderControl(writer);
}
}
Note: now build the project to compile and generate an assembly which will have the name “AspControls.dll”."," In CustomControls, CreateChildControls method should be used for writing the logic for creating the child
controls, setting the properties and binding event handlers to events as we performed in the above code.
CreateChildControls method is defined as “virtual” in “Control” class which is a parent class for all Controls, so we
have overriden that method and implemented the logic for creating TextBox, ImageButton and Calendar."," EnsureChildControls method is typically used in Custom Controls which use child controls for
functionality. The EnsureChildControls method is called in order to make sure that child controls have been
created and are ready to process input, to perform data binding, or to perform other tasks. This method first
checks whether the child controls are created or not and if not created then CreateChildControls method is called."," 5
\f Every Asp.Net Server Control contains RenderControl method which is automatically called by the page
during rendering, which outputs server control content to the provided HtmlTextWriter object. This method is
defined as “virtual” in “Control” class which is a parent class for all Controls and Custom Control developers should
override this method and implement the logic for rendering their control. We have overriden that method in our
Control and called the RenderControl method of TextBox, ImageButton and Calendar because those controls
already contain that method.
Consuming the Custom Control: the advantage with Custom Controls is they can be added to ToolBox and then we
can “Drag and Drop” them on any WebForm and to do that go back to our regular project i.e. “ControlsDemo”,
open the ToolBox window, right click on it and choose the option “Add Tab” which will add a new tab, specify a
name to it for example: “NIT Controls”. Now right click on the new tab and select the option “Choose Items” which
will open “Choose ToolBox Items” window, click on the “Browse” button on it and select “AspControls.dll” from its
physical location and click “Ok” which will add “CustomCalendar” under the new tab.","Registering the Custom Control Page level: to register the Custom Control page level use the following statement
on a WebForm below page directive:
<%@ Register Assembly=\"AspControls\" Namespace=\"AspControls\" TagPrefix=\"NIT2\" %>","Registering the Custom Control Application level: to register the Custom Control application level open the
Web.config file and write code under tag where we registered “CalendarUserControl” which should
now look as following:





Now add a new WebForm naming it as “TestCustomCalendar.aspx” and write the following code under
tag:",""," Date of Birth:"," "," "," "," 6
\f Mobile No:
Address:






Now goto TestCustomCalendar.aspx.cs file and write the following code
Code under Page_Load:
if (!IsPostBack) { txtName.Focus(); ccDate.SelectedDate = DateTime.Today; }
Code under Submit Data Button:
Response.Write(\"\");
if (rbMale.Checked) { Response.Write(\"\"); }
else if (rbFemale.Checked) { Response.Write(\"\"); }
Response.Write(\"\");
Response.Write(\"\");
Response.Write(\"\");
Code under Reset Form Button:
txtName.Text = txtEmail.Text = txtMobile.Text = \"\"; rbMale.Checked = rbFemale.Checked = false;
ccDate.Reset(); txtName.Focus();
Developing a VideoPlayer control as a Custom Control:
Most of the existing Html controls are developed by Microsoft as “Asp.Net Server Controls” and given for
us to consume and all those controls will finally render Html, whereas controls introduced in “Html 5” like