Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,
I want to add user control(.ascx) inside repeater multiple times on linkbutton click(inside same repeater). Would anyone plz help me doing that. Im stuck doing this since last two days
plzz. any help would be appreciated. Thanks
Posted
Comments
Sanjay K. Gupta 2-Aug-13 5:02am    
What you have tried?
Where you heve stucked?
What error you are getting?
[no name] 2-Aug-13 5:47am    
I have a repeater, in which i placed a linkbutton and a placeholder. what i want is when user clicks on linkbutton, user control(that i created in UserControls folder) get added in placeholder as many times as button is clicked by user in current repeater item.
Till now i'm able to add multiple user controls on linkbutton click. but when i click some other button or link, page is posted back and all those dynamic user controls just disappear.

Next, when i again click linkbutton, number of user control before post back + 1 user controls loaded again.

following is my code below :
// page load
protected void Page_Load(object sender, EventArgs e)
{
if (Session["TherapistName"] == null)
{
Response.Redirect("~/Logon.aspx");
}
if (!IsPostBack)
{

FillRepeaterInterventions();
ArrayList ar = new ArrayList();
ViewState["array"] = ar;
foreach (RepeaterItem item in rptInterventions.Items)
{
int itemIndex = item.ItemIndex;
PlaceHolder placeholder = (PlaceHolder)item.FindControl("Placeholder1");
LoadUserControl(placeholder, itemIndex);
}
}
}
//protected void rptInterventions_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "AddIntervention")
{
int itemIndex = e.Item.ItemIndex;
PlaceHolder placeholder1 = e.Item.FindControl("PlaceHolder1") as PlaceHolder;
int count = ((ArrayList)ViewState["array"]).Count;
((ArrayList)ViewState["array"]).Add(count + 1);
LoadUserControl(placeholder1, itemIndex);
}
}

// load user control method

protected void LoadUserControl(PlaceHolder placeholder1, int itemIndex)
{

placeholder1.Controls.Clear();
if (((ArrayList)ViewState["array"]).Count > 0)
{
for (int i = 1; i <=((ArrayList)ViewState["array"]).Count; i++)
{
UserControls_ucNewIntervention ucNewIntervention = LoadControl("~/UserControls/ucNewIntervention.ascx") as UserControls_ucNewIntervention;
ucNewIntervention.ID = "webUC" + itemIndex + i.ToString();
placeholder1.Controls.Add(ucNewIntervention);
}
}
}

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900