jshbrmn
jshbrmn

Reputation: 1787

Why is ASP inserting text to name attributes on input elements?

So I'm working through validating my forms in asp.net and I noticed the rendered form input elements are changing the "name" attribute from something like name="firstName" to name="ctl00$ContentPlaceHolder1$txtFN".

It appears to be grabbing the id of the input element as well as some other text which at this point I can't entirely make sense of.

For reference:

this is the rendered element -

<input name="ctl00$ContentPlaceHolder1$txtEmail" type="text" id="ContentPlaceHolder1_txtEmail" class="reqT"> 

and this is what I am setting up initially -

<asp:TextBox ID="txtFN" class="reqT" runat="server" name="firstName"></asp:TextBox>   

Would anyone be able to clarify: - the significance of this insert - is it changeable, and if so what sort of effects does this have

Thanks!

Upvotes: 0

Views: 229

Answers (1)

Tieson T.
Tieson T.

Reputation: 21191

ASP.NET does that to insure completely unique names on input elements. You can disable it by changing the ClientIDMode to Static.

Scott Gu explains a few ways to do that here: http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx

I personally find the web.config option to be the easiest:

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

Upvotes: 3

Related Questions