Taylor Brown
Taylor Brown

Reputation: 1776

Why does the 'name' attribute on my hidden input element change from what I set it to be?

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)

Upvotes: 2

Views: 1893

Answers (3)

McGarnagle
McGarnagle

Reputation: 102743

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" ... %>

Upvotes: 3

Venkata Krishna
Venkata Krishna

Reputation: 15112

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

Upvotes: 3

Karl Anderson
Karl Anderson

Reputation: 34846

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.

Upvotes: 1

Related Questions