I have a user control with a WebDateTimeEdit server control on it (essentially a TextBox). I’d like to be able to set some of the server control’s properties by way of the user control’s markup or programatically. for example I have this defined on my user control:
public string CssClass
{
get { return this.WebDateTimeEdit1.CssClass; }
set { this.WebDateTimeEdit1.CssClass = value; }
}
which allows me to do
<uc1:MyControl ID="MyControl1" runat="server" CssClass="fancycss" />
and this works. but if I want to add a control through code,
MyControl myControl2 = new MyControl();
MyControl.CssClass = "fancycss";
this blows up because WebDateTimeEdit1 is null.
is there any way I can make this happen? also can do I do this
myControl2 .Font.Size = FontUnit.Point(8);
where the Font property is read only on the server control?
It depends on how your control is defined (is it a
CompositeControl?), But generally speaking, you want to callEnsureChildControls()as the first line of the setter (and likely the getter too)