开发者

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 insid开发者_JAVA技巧e 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.


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


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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜