ADDING USER CONTROLS FROM CODE BEHIND

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
C# 
 861  View(s)
Ratings:
Rate this:

I have a user control(UserControl1) on my page and one Add more button.Clicking on Add More Button add the same user control again.When i click on user control first time,it adds second User Control,but on second time it doesnot add other.I think the control gets lost. I am adding a control like this on link button click:-

 

protected void lnkadd_Click(object sender, EventArgs e)

{

    HtmlTableCell tCell = new HtmlTableCell();

    UserControl uc = (UserControl)Page.LoadControl("~/Controls/DirectionalPricingCtrl.ascx");

    tCell.Controls.Add(uc);

    tablerow.Cells.Add(tCell);

}

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Adding User Controls from code behind

    Hi Ashish, 


    Your code to add controls programmatically is right. Try doing,

    tablerow.Cells.Add(tCell);

    instead of adding it to the controls. this looks like a control rendering issue.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!