...">

Is ASP.NET name matching consistent?

When you have an ASP control as follows:

<asp:TreeView ID="TreeItems" runat="server"></asp:TreeView>

The generated html manages the names. If I want to access the identifiers of the generated elements directly, I can try to find out that it is distorting the names and looking for that identifier.

Are the names of the generated elements guaranteed to be executed in a certain way according to any standard set by Microsoft? I'm just afraid of this hack if they release a new version of .NET that does it differently. Is there a way to generate a name that controls itself in code?

+3
source share
4 answers

Consistency is not guaranteed (this is an implementation detail)

ClientID ( ).

+5

script, :

<script type="text/javascript">
    //initialize client names of server side controls.
    var TreeItemsId = "<%= TreeItems.ClientID  %>";    
</script>

, :

<script type="text/javascript">
    //initialize client names of server side controls.
    var TreeItemsId = "ctl00_TreeItems";    
</script>
+2

, , .

It should be consistent as long as the same version of the framework is used, but updates on the server can change this, so you should not rely on the knowledge of the generated name.

Use the property ClientIDto find out the generated control identifier.

0
source

Source: https://habr.com/ru/post/1758093/


All Articles