Last post Aug 07, 2009 04:33 PM by Syphon
Aug 07, 2009 12:07 PM|Syphon|LINK
I've made a simple custom control that inherits from a TextBox. The class looks like so
public class TextBox2 : System.Web.UI.WebControls.TextBox
protected override void OnInit( EventArgs e )
Text = "This is the default text that should not appear";
base.OnInit( e );
Now on my aspx page during the Page_Init I create a new TextBox2 and add it to a placeholder. The code looks like so
protected void Page_Init(object sender, EventArgs e)
TextBox2 txt = (TextBox2) LoadControl(typeof (TextBox2), null);
txt.Text = "Hello World";
txt.ID = "txtBox";
Now as I understand the LoadControl method, it should raise all of the added control's events until it has caught up to the current event. The expected behavior would be that LoadControl would fire the Init event of the the custom control, thus setting the
text to "This is the default text.....", then during the Page_Init the text value would be set to "Hello World". However the Init event isn't firing until I add the control to the placeholder.
What am I doing wrong here?
Aug 07, 2009 02:59 PM|PNasser|LINK
You are almost correct. It plays catch up once it's added to the controls collection.
Ify ou move your phTest.controls.Add(txt); then do the txt.Text and txt.Id it should work as you expect it to
Aug 07, 2009 04:33 PM|Syphon|LINK
That is such a "good-to-know" thing.