Monday, 23 December 2013

Create a Custom Calender control in ASP.Net

In this article we are to see how to create the custom calendar control in asp.net, for that first add the server control file from the new item. Then modify the file based on the following code , Toolbox data specify how the script will looks when drag and drop from toolbox.

Override the CreateChildControls method and add the new controls to the collection.
Override the Render method and render the control that which are you want to render.


  [ToolboxData("<{0}:CustomCalender runat=server></{0}:CustomCalender>")]
    public class CustomCalender : CompositeControl
    {
        TextBox txt;
        ImageButton img;
        Calendar cal;

        protected override void CreateChildControls()
        {
            Controls.Clear();
            txt = new TextBox();
            txt.ID = "dateTextbox";
            txt.Width = Unit.Pixel(80);

            img = new ImageButton();
            img.ID = "CalenderImageButton";

            cal = new Calendar();
            cal.ID = "CalenderControl";

            this.Controls.Add(txt);
            this.Controls.Add(img);
            this.Controls.Add(cal);

            //base.CreateChildControls();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            txt.RenderControl(writer);
            img.RenderControl(writer);
            cal.RenderControl(writer);
        }
    }


Now after the build the control will look on the right side of the toolbox and drag and drop the control which will create a custom calendar control.I hope this article will clear you the how to create a custom calender control.

No comments:

Post a Comment