开发者

How do you assign a specific NAME to a tag when using master pages?

开发者 https://www.devze.com 2022-12-22 00:03 出处:网络
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.

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 whatsoe开发者_运维技巧ver. 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?


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)


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消