views:

32

answers:

2

HI ,

1.create Base usercontrol which contains a label and sets text of it to be "test1" this is set in the ascx page. 2.Create a user control which inherits from the base user control. 3. Now on the page load of the child control i try to do:

protected override void Page_Load(object sender, EventArgs e) { //Exception thrown by this as Label1 is null this.Label1.Text = "sasasas";

}

However i get an exception because the Label1 is null.

Can aonyone tell me how to initialize the base controls controls when the child control is created?????

thanks Niall

A: 

Hi NBrowne,

Try calling EnsureChildControls() before you access the label. Cheers Tigger

Tigger
thanks Tigger i tried that but it didnt solve the problem.
NBrowne
+1  A: 

As RPM1984 commented: do not inherit from your Webusercontrol but nest it inside of your other Usercontrol. When you have exposed a Control(f.e. your Label or its Text property) as property in the "parent"-UC, you can access it from your "child"-UC.

Besides, always use LoadControl to initialize an dynamically added Usercontrol and not the constructor (what might be the cause that the Textbox is NULL).

Tim Schmelter
Thanks for that. Ya I could have done that but because I wanted inheritance based control to enforce required methods etc I modified my base control to not be a user control but to inherit from system.we.ui.usercontrol instead.
NBrowne