Jason
Jason

Reputation: 11615

Disabling Asp.Net Auto Naming

Is there a way to disable the ASP.Net auto-naming?

For instance: When I have a control on my page that sits inside a masterpage instead of being named

theLabel

it is renamed to

ctl00_ContentPlaceHolder1_clist0_rptSelected_ctl05_theLabel

This is the behavior I don't want.

Upvotes: 2

Views: 1339

Answers (3)

Joe Ratzer
Joe Ratzer

Reputation: 18549

If you're using ASP.NET 4.0 then yes you can.

Otherwise, it's difficult. If you want the client id in javascript you can do something like this::

<script type="text/javascript">
    function DoSomething(){
        alert('<%= Control.ClientID %>');
    }
</script>

Upvotes: 1

Dave Swersky
Dave Swersky

Reputation: 34810

This is a common headache with ASP.NET. If ASP.NET 4 is an option for you, you can use the new ClientID functionality to customize the naming convention.

More information, courtesy of The Gu:

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

Upvotes: 5

Larsenal
Larsenal

Reputation: 51166

If you're lucky enough to be able to target ASP.NET 4.0...

In your page directive, set ClientIDMOde="Static". This will emit ID's exactly as you have them in your controls.

So if you have:

<asp:Label ID="example" runat="server" />

It will emit this clean ID:

<span id="example">something...</span>

Upvotes: 4

Related Questions