Question

I'm trying to create a server control, which inherits from TextBox, that will automatically have a CalendarExtender attached to it. Is it possible to do this, or does my new control need to inherit from CompositeControl instead? I've tried the former, but I'm not clear during which part of the control lifecycle I should create the new instance of the CalendarExtender, and what controls collection I should add it to. I don't seem to be able to add it to the Page or Form's controls collection, and if I add it to the (TextBox) control's collection, I get none of the pop-up calendar functionality.

Was it helpful?

Solution

I accomplished this in a project a while back. To do it I created a CompositeControl that contains both the TextBox and the CalendarExtender.

In the CreateChildControls method of the CompositeControl I use code similar to this:

TextBox textbox = new TextBox();
textbox.ID = this.ID + "Textbox";
textbox.Text = this.EditableField.TextValue;
textbox.TextChanged += new EventHandler(HandleTextboxTextChanged);
textbox.Width = new Unit(100, UnitType.Pixel);
CalendarExtender calExender = new CalendarExtender();
calExender.PopupButtonID = "Image1";
calExender.TargetControlID = textbox.ID;
this.Controls.Add(textbox);
this.Controls.Add(calExender);

Of course make sure that the form containing this CompositeControl has a toolkit script manager.

OTHER TIPS

I know this is an old thread, but I came across it when I had a similar question. This is what I ended up implementing, and it works great. If you want the control to BE a TextBox, then simply pump out the extender during the call to Render.

Imports System.Web.UI.WebControls
Imports AjaxControlToolkit

Public Class DateTextBox
    Inherits TextBox

    Private _dateValidator As CompareValidator
    Private _calendarExtender As CalendarExtender

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)

        _dateValidator = New CompareValidator
        With _dateValidator
            .ControlToValidate = ID
            Rem set your other properties
        End With
        Controls.Add(_dateValidator)

        _calendarExtender = New CalendarExtender
        With _calendarExtender
            .TargetControlID = ID
        End With
        Controls.Add(_calendarExtender)
    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
        _dateValidator.RenderControl(writer)
        _calendarExtender.RenderControl(writer)
    End Sub
End Class

You can easily add ajax calendar in custom server controls. You need to add two reference in your application. 1. AjaxControlToolkit.dll 2. System.Web.Extensions With the help of second reference we will get all the property of “CalendarExtender” in your custom server controls.

When you are trying to not allow users to type anything in the textbox, but only be filled by the calendar extender and then you try to get the selected date from the textbox control it may be empty string if you have set the textbox property to ReadOnly="True".

Its because read only controls are NOT posted back to the server. Workaround for this is the following:

protected void Page_Load(object sender, EventArgs e)

{

TextBox1.Attributes.Add("readonly", "readonly");

}

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top