2

Here is my markup:

<input type="hidden" runat="server" name="block" id="FSC_show_sidebar_button" value="0" />
<input type="hidden" runat="server" name="block" id="FSC_hide_sidebar_button" value="1" />

This is what it looks like when the page is rendered and I inspect it:

<input name="ctl00$MainContent$FSC_show_sidebar_button" id="ctl00_MainContent_FSC_show_sidebar_button" type="hidden" value="0"/>
<input name="ctl00$MainContent$FSC_hide_sidebar_button" id="ctl00_MainContent_FSC_hide_sidebar_button" type="hidden" value="1"/>

Is there a way to keep the 'name' attribute from changing? (Not the ID I do not care if this changes)

Taylor Brown
  • 1,689
  • 2
  • 17
  • 33

3 Answers3

3

This is a naming convention that ASP.Net uses to convert the ID property you set to a client ID. You can modify this behavior by setting the ClientIDMode property. By default this is set to "Predictable", which means:

The ClientID value is generated by concatenating the ClientID value of the parent naming container with the ID value of the control.

To make ASP.Net use the ID exactly as you specify, set ClientIDMode=Static. You can set this globally in web.config:

<system.web>
    <pages clientIdMode="Static" ... />
</system.web>

Or at the page (or individual control) level:

<%@ Page ClientIDMode="Static" ... %>
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • thanks, seconds after posting this i researched using different words and found this: http://stackoverflow.com/questions/5792290/input-name-and-id-changes-when-set-runat-server ... But I will mark yours as answer when the 12 min time is up. – Taylor Brown Aug 16 '13 at 20:53
  • 1
    Actually, I am asking about the NAME not the ID field... name still changes with this added. – Taylor Brown Aug 16 '13 at 20:58
  • 1
    @taybriz ohh ... The thing is, the ASP.Net pipeline uses the `name` to reconstruct the server state from the HTML. Maybe that can't be changed ... – McGarnagle Aug 16 '13 at 21:01
  • 1
    @taybriz I think @dumass is right -- you have to either remove `runat=server` making them client controls, or accept the mangling of the name. – McGarnagle Aug 16 '13 at 21:03
3

Due to the runat="server" attribute, this becomes a server side control. This would prepend your masterpage, control information to your input element.

Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
  • that's a shame I was hoping I could find a way to make it static without removing the server side access. ok, thanks everyone. – Taylor Brown Aug 16 '13 at 21:05
1

This is because your element is inside of a master page, the master page uniquely identifies controls by naming it by containers.

MainContent is the name of your content placeholder.

To avoid this name mangling, then you need to use the ASP.NET 4.0 ClientIDMode attribute.

Read Control.ClientIDMode Property for more information on the ClientIDMode attribute.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80