I have several user controls, let's say A
, B
, C
and D
. Based on some random input, I need to generate a combination of these. For e.g. if input is 2a3d1a2c
I need to show two of the A's, 3 D's after that, an A again, etc.
我有几个用户控件,比如A,B,C和D.基于一些随机输入,我需要生成这些组合。对于例如如果输入是2a3d1a2c,我需要显示A之后的两个,之后是3个D,再次是A等等。
I will also need to stabilize clientid's in order for them to work correctly. Because each of these controls use their own ClientID
property to gather the inputs on themselves. For e.g. user control A internally generates an input named this.ClientID + "$input1"
, and gathers its input from request like Request[this.ClientID + "$input1"]
. Since there can be more than one A, each A needs to have the same (unique) ClientID
after postback in order to get correct inputs from request.
我还需要稳定clientid以使它们正常工作。因为每个控件都使用自己的ClientID属性来收集自身的输入。对于例如用户控件A在内部生成一个名为this.ClientID +“$ input1”的输入,并从Request [this.ClientID +“$ input1”]等请求中收集其输入。由于可以有多个A,因此每个A在回发后需要具有相同的(唯一的)ClientID,以便从请求中获得正确的输入。
1 个解决方案
#1
0
To add controls dynamically, you can use a panel as a place holder, say
要动态添加控件,您可以使用面板作为占位符
<asp:Panel ID="ControlPlaceholder" runat="server" />
Then, on the server side, you can add objects to it like so:
然后,在服务器端,您可以像这样添加对象:
int controlCount = 0;
...
TextBox newTextBox = TextBox();
newTextBox.ID = "ctl_" + controlCount++;
ControlPlaceholder.Controls.Add(newTextBox);
If you add the controls to it during your Page_Load event and use a consistent method of generating the controls' IDs (such as a the simple count above) then any viewstate or event bindings will be re-bound to the right object on a postback.
如果在Page_Load事件期间向其添加控件并使用生成控件ID的一致方法(例如上面的简单计数),则任何视图状态或事件绑定将在回发时重新绑定到正确的对象。
#1
0
To add controls dynamically, you can use a panel as a place holder, say
要动态添加控件,您可以使用面板作为占位符
<asp:Panel ID="ControlPlaceholder" runat="server" />
Then, on the server side, you can add objects to it like so:
然后,在服务器端,您可以像这样添加对象:
int controlCount = 0;
...
TextBox newTextBox = TextBox();
newTextBox.ID = "ctl_" + controlCount++;
ControlPlaceholder.Controls.Add(newTextBox);
If you add the controls to it during your Page_Load event and use a consistent method of generating the controls' IDs (such as a the simple count above) then any viewstate or event bindings will be re-bound to the right object on a postback.
如果在Page_Load事件期间向其添加控件并使用生成控件ID的一致方法(例如上面的简单计数),则任何视图状态或事件绑定将在回发时重新绑定到正确的对象。