使用母版页时如何将特定名称分配给标记?

发布于 2024-08-23 03:15:20 字数 672 浏览 3 评论 0 原文

我正在使用母版页,并且正在使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病女 2024-08-30 03:15:20

System.Web.UI.WebControls.Control 有一个名为 ClientIDMode 的属性。
您可以使用 HiddenField,而不是 HtmlInputHidden

 'Name
Dim hidName As New System.Web.UI.WebControls.HiddenField
hidName.ID = "item_name_" & count.ToString
hidName.ClientIDMode = System.Web.UI.ClientIDMode.Static
hidName.Value = item
Form.Controls.Add(hidName)

请参阅在 ASP.NET 中隐藏文本框HiddenField 类

ClientIDMode 是在 .Net Framework 4.0 中引入的。
对于早期版本,替代方法可以是添加 asp:Literal

 'Name
Dim hidName As New System.Web.UI.WebControls.Literal
hidName.Text = _
  String.Format("<input name=""item_name_{0}"" type=""hidden"" value=""{1}""/>", _
                count, item)
Form.Controls.Add(hidName)

System.Web.UI.WebControls.Control has a property called ClientIDMode.
Instead of HtmlInputHidden, you may use a HiddenField.

 'Name
Dim hidName As New System.Web.UI.WebControls.HiddenField
hidName.ID = "item_name_" & count.ToString
hidName.ClientIDMode = System.Web.UI.ClientIDMode.Static
hidName.Value = item
Form.Controls.Add(hidName)

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.

 'Name
Dim hidName As New System.Web.UI.WebControls.Literal
hidName.Text = _
  String.Format("<input name=""item_name_{0}"" type=""hidden"" value=""{1}""/>", _
                count, item)
Form.Controls.Add(hidName)
油焖大侠 2024-08-30 03:15:20

不幸的是,简单的答案是否定的。更困难的答案是肯定的,但不是以一种直接的方式。解决方法是不尝试设置属性,而是将名称定义为属性:

Dim hidName As New HtmlInputHidden
hidName.Attributes("Name") = "item_name_" & count.ToString
hidName.Value = item
Form.Controls.Add(hidName)

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:

Dim hidName As New HtmlInputHidden
hidName.Attributes("Name") = "item_name_" & count.ToString
hidName.Value = item
Form.Controls.Add(hidName)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文