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.
+2
A:
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.
Joseph Daigle
2008-09-17 14:40:33
Is it then safe to assume that I'll need to implement and forward any properties to the appropriate child control. For example, if I want the Text property from the TextBox, I implement it in the CompositeControl and forward calls to the child TextBox?
LockeCJ
2008-09-17 15:02:19
Basically yes. However you'll want to then have internal TextBox be an instance member (which might have to be instantiated prior to CreateChildControls) of your CompositeControl unlike how my code handles it.
Joseph Daigle
2008-09-17 17:59:41
A:
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
spot
2010-02-12 20:07:07