0

i have a web form in to which i load a web user control(A)

an when a button on control A is clicked it loads an array of another web user control named as infobox

when i add the user control in to control collection of web user control A it does not show.

below is the code: on button click of control A

foreach (Categories category in CategoriesDataList)
            {
                InfoBox ib = new InfoBox();
                ib.LiteralName = category.Category_Name;
                span_tempList.Controls.Add(ib);
                ib = null;           
            }   



span_tempList is a `<span>` tag with `runat=server`

below i HTML and cs Code for InfoBox Control

 public partial class InfoBox : System.Web.UI.UserControl
    {
        protected global::System.Web.UI.WebControls.Label ltrlName = new System.Web.UI.WebControls.Label();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public InfoBox()
        {

        }

        public string LiteralName
        {
            get { return ltrlName.Text; }
            set { ltrlName.Text = value; }
        }

    }

--HTML--

<div style="width:10%;float:left;">
        <asp:Label ID="ltrlName" runat="server" Text="Label"></asp:Label>
    </div>
    <div style="width:20%;float:left;">
        <asp:Image ID="imgPicture" runat="server" ImageUrl="" />
    </div>
    <div style="width:70%;float:right;">

    </div>

also tried to add controls to page doesn't works

1
  • Have you checked to see if the IsPostBack property of the page in the Page constructor true or false? Commented Jan 5, 2012 at 13:26

1 Answer 1

3

You should not create an instance of a UserControl via constructor but by using LoadControl if you want to add it to the page's control-collection.

InfoBox ib = (InfoBox)LoadControl("InfoBox.ascx");

Also, why are you setting the control to null? It will be disposed automatically at the end of the page-lifecycle.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks For the help. it helped me and was able to load control programatically.. thanks but "InfoBox.ascx.cs" gave me error it was only .ascx file path LoadControl was asking for.

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.