I have created a user control which basically consists of 3 text boxes. I want to allow the user to click a hyperlink which will add a new instance of the user control onto a PlaceHolder. This seems to be working as I populate one of the text box controls with a random number which changes whenever I click the hyperlink. However, it is overwriting the previous control.
我创建了一个用户控件,它基本上由3个文本框组成。我希望允许用户单击一个超链接,该超链接将在占位符上添加用户控件的新实例。当我用一个随机数填充其中一个文本框控件时,这似乎起了作用,每当我单击超链接时,这个随机数就会改变。但是,它覆盖了以前的控件。
Heres 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 what I need to know is how can I get the control adding different instances underneath eachother as it just seems to be 1 control being overwritten each time.
基本上,我需要知道的是如何让控件在每个实例下面添加不同的实例,因为每次都有一个控件被覆盖。
Thanks.
谢谢。
1 个解决方案
#1
2
The problem is after the postback, your previously added controls are not added to the placeholder. Dynamically added control should be added on each postback. My recommendation is to store a counter in a variable and at your page load, add your web controls again depending to this counter.
问题是在回发之后,先前添加的控件不会添加到占位符中。应该在每个回发中添加动态添加控件。我的建议是在变量中存储一个计数器,在页面加载时,根据这个计数器再次添加web控件。
#1
2
The problem is after the postback, your previously added controls are not added to the placeholder. Dynamically added control should be added on each postback. My recommendation is to store a counter in a variable and at your page load, add your web controls again depending to this counter.
问题是在回发之后,先前添加的控件不会添加到占位符中。应该在每个回发中添加动态添加控件。我的建议是在变量中存储一个计数器,在页面加载时,根据这个计数器再次添加web控件。