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); }I think i am doing something wrong and i should add user controls in respect of page life cycle,but how?Can somebody please guide me or provide some useful links? What approach i should follow when i have to add a user control each time when i click on a Add button and then later retrieve all the values and save it to DB?
Pratap PatelPosted Mar 31, 2014, 5:48 AM
You can create a list of UserControl as I described below,
List
then on Button click event, you write logic to add the same new user control at different index of list by using the Insert(index,usercontrol) method of the List class.
I changed your code specify in question and I hope it will work,
//Global List variable
private List
int i = 0;
protected void lnkadd_Click(object sender,EventArgs e)
{
i++;
lstUc.Insert(i,(UserControl)Page.LoadControl("~/Controls/DirectionalPricingCtrl.aspx"));
}