0

I created user control and want to add it to page. I use next code:

Controls_MultiTextInput cc = new Controls_MultiTextInput();
Controls.Add(cc);

But control doesn't appear on page. What is wrong?

2
  • I am able to add control using this.Controls.Add(this.LoadControl("MultiTextInput.ascx")); But it seems a bad way, because I was not able to set properties of control before adding. Commented Aug 23, 2010 at 9:41
  • Does it work when you add a non-custom control, eg a TextBox? Commented Aug 23, 2010 at 9:47

2 Answers 2

1

this.Controls.Add(this.LoadControl("MultiTextInput.ascx")) is the correct way to load the control because it needs to know where the ascx file is.

If you want to set properties, do this:

Controls_MultiTextInput cc = (Controls_MultiTextInput) LoadControl("MultiTextInput.ascx");
cc.MyProperty = "abc";
Controls.Add(cc);
Sign up to request clarification or add additional context in comments.

Comments

0

Anton you can set properties !

Controls_MultiTextInput cc 
   = (Controls_MultiTextInpu)Page.LoadControl("MultiTextInput.ascx");

cc.variable = 2;
cc.SetProperties(223,2311);

Controls.Add(cc);

or

PlaceHolder.Controls.Add(cc);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.