我正在使用母版页,并且正在使用 Google Checkout 所需的名称在表单上动态添加隐藏文本框。
<input name="item_name_1" type="hidden" value="Widget #1"/>
使用 VB.NET,我执行以下代码,
'Name
Dim hidName As New HtmlInputHidden
hidName.ID = "item_name_" & count.ToString
hidName.Value = item
Form.Controls.Add(hidName)
但是因为我使用母版页,所以该控件被重命名为“ctl00$item_name_1”。
<input name="ctl00$item_name_1" type="hidden" id="ctl00_item_name_1"
请注意,我尝试设置名称属性 (hidName.Name = "item_name_" & count.ToString),并尝试将名称添加到属性列表中。奇怪的是,这对名称属性没有任何影响。当我不使用母版页时,我注意到当我设置 ID 属性时,NAME 会自动分配相同的值。
当您使用母版页时,有没有办法控制动态添加的控件的名称?
I am using Master Pages and I am truing to dynamically add hidden text boxes on the form with the NAMEs that Google Checkout expects.
<input name="item_name_1" type="hidden" value="Widget #1"/>
Using VB.NET, I execute the following code
'Name
Dim hidName As New HtmlInputHidden
hidName.ID = "item_name_" & count.ToString
hidName.Value = item
Form.Controls.Add(hidName)
But because I use Master Pages, the control is renamed to "ctl00$item_name_1".
<input name="ctl00$item_name_1" type="hidden" id="ctl00_item_name_1"
Note that I tried to set the Name property (hidName.Name = "item_name_" & count.ToString) and also tried to add the name to the Attributes list. This strangely had no effect on the name attribute whatsoever. When I am not using master pages, I notice that when I set the ID property the NAME is automatically assigned the same value.
Is there a way to control the name of a dynamically added control when you are using master pages?
发布评论
评论(2)
System.Web.UI.WebControls.Control
有一个名为ClientIDMode
的属性。您可以使用
HiddenField
,而不是HtmlInputHidden
。请参阅在 ASP.NET 中隐藏文本框和HiddenField 类。
ClientIDMode
是在 .Net Framework 4.0 中引入的。对于早期版本,替代方法可以是添加
asp:Literal
。System.Web.UI.WebControls.Control
has a property calledClientIDMode
.Instead of
HtmlInputHidden
, you may use aHiddenField
.See Making text box hidden in ASP.NET and HiddenField Class.
The
ClientIDMode
was introduced in the .Net Framework 4.0.For earlier versions, an alternative could be adding a
asp:Literal
.不幸的是,简单的答案是否定的。更困难的答案是肯定的,但不是以一种直接的方式。解决方法是不尝试设置属性,而是将名称定义为属性:
Unfortunately, the simple answer is No. The more difficult answer is Yes, but not in a straightforward way. A workaround is not to try to set the property but instead define the name as an attribute: