Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 966
Chad Avatar asked Nov 14 '22 12:11

Chad


1 Answers

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)
like image 184
tiago2014 Avatar answered Dec 19 '22 09:12

tiago2014