.Net Changes the element IDs

前端 未结 5 1472
离开以前
离开以前 2020-12-19 19:03

.Net is kindly changing the element ids on my pages by appending a ct100_ to them. As much as I appreciate microsoft trying to help me keep from duplicating ids on my site,

相关标签:
5条回答
  • 2020-12-19 19:33

    You will need to:
    override regular controls' behavior to decouple the Control.UniqueID property from the Control.ID property
    override naming container controls to allow us to control how ID generation is done and how to find a control

    References:
    http://nunogomes.net/post/2008/06/02/ASPNET-Controls-Improving-automatic-ID-generation-Architectural-Changes-(-Part-3).aspx


    http://weblogs.asp.net/nunogomes/archive/2008/06/04/asp-net-controls-improving-automatic-id-generation-architectural-changes-part-3.aspx


    http://forums.asp.net/t/1394822.aspx


    0 讨论(0)
  • 2020-12-19 19:41

    Any control which has the INamingContainer interface on it will get the control heirarchy appended to it to allow for multiple controls to be on the page without conflicting. You should be using the ClientID property of the control if you wish to know what the id of the element will be on the client.

    0 讨论(0)
  • 2020-12-19 19:44

    That's just how aspnet works. Controls provide the clientid method for you to use in your code behind for this reason.

    If you want to refer to objects from js, you can either inject the clientid or use classes or other attributes.


    Edit: Note that this only applies to the ASP.NET controls. If you use the HTML controls, the given IDs are preserved. You can access them in your code behind by adding the runat=server attribute to them, too. Obviously these controls break the webforms model with viewstate, etc. but they do give you your desired functionality.

    Of course it's been a while since I worried about it so I could be wrong...(please comment or edit if I am!).

    0 讨论(0)
  • 2020-12-19 19:52

    You cannot prevent this in the current version of ASP.NET - the next version will allow you to do this. Perhaps ASP.NET MVC is a good choice for you?

    0 讨论(0)
  • 2020-12-19 19:56

    to not use anything on the server side.

    This is an inherent aspect of the ASP.NET system, and there is no way to use .NET Server controls and not have the prefixes appended.

    To access the client-side name, you can use the myControl.ClientID property.

    0 讨论(0)
提交回复
热议问题