ASP.NET Controls - Multiple Instance Software Problem?

I created a user control that basically consists of 3 text fields. I want to allow the user to click a hyperlink that will add a new instance of the user control to PlaceHolder. This seems to work when I populate one of the textbox controls with a random number, which changes whenever I click on the hyperlink. However, this overwrites the previous control.

Here is the code on MyPage.aspx

protected void MyHyperlink_Click(object sender, EventArgs e)
{
     var uc = new MyUserControl();
     uc = (MyUserControl)LoadControl("~/path/to/my/usercontrol.ascx");
     placeHolderCtrl.Controls.Add(uc);
}

Basically, I need to know how I can get a control that adds different instances under eachother, because it just seems like every control is overwritten every time.

Thanks.

+3
source share
1 answer

The problem is that after the postback, previously added controls are not added to the placeholder. Dynamic added control should be added at each postback. My recommendation is to store the counter in a variable, and when the page loads, add web controls again depending on that counter.

+2
source

Source: https://habr.com/ru/post/1717189/


All Articles