Change control identifier with 'ct100' prefix in asp.net

Checking my control ID is ContentPlaceHolder1_lnkDistributors .

But, after placing my site on a real server, the prefix adds to the ID above.

like: ctl00_ContentPlaceHolder1_lnkDistributors .

<asp:LinkButton ID="lnkDistributors" runat="server" Text="Distributors"> </asp:LinkButton> 

Please help me. Thanks in Adv.

+4
source share
2 answers

Static ClientIDMode is designed to control ID in HTML; but you should use it carefully: instead, it does not guarantee the uniqueness of the control identifier on the page.

 <asp:LinkButton ID="lnkDistributors" ClientIDMode="Static" runat="server" Text="Distributors"> </asp:LinkButton> 

If your control has its ClientIDMode set to AutoID , the control identifier will have a prefix to make it unique. (for example, control with identifier "sam" will become "ct100_sam"). If your control has its ClientIDMode set to Static , the identifier will not be changed and will remain depending on what the developer has set for it.


MSDN

ASP.NET provides several algorithms for creating the value of the ClientID property. You choose which algorithm to use for the control by setting its ClientIDMode property. The algorithms are identified by the ClientIDMode enumeration values ​​listed in the following list.

Autoid

The ClientID value is generated by combining the identifier values ​​of each parent naming container with the identity value control. In data binding scenarios where multiple instances of the control are rendered, an extra value is inserted before the identifier value of the control. Each segment is separated by an underscore character (_). This algorithm was used in versions of ASP.NET earlier than ASP.NET 4.

Static

The ClientID value is set to the value of the ID property. If the control is a named container, the control is used as the top container naming hierarchy for any controls that it contains.

Predicted

This algorithm is used for data-related controls. The ClientID value is generated by concatenating the ClientID value of the parent naming container with the identity value control. If the control is a data-bound control that generates multiple rows, the value of the data field specified in the ClientIDRowSuffix property is added at the end. You can specify multiple data fields for a GridView. If the ClientIDRowSuffix property is empty, the sequence number is added to the end instead of the data field value. This number starts from zero and increases by 1 for each line. Each segment is separated by an underscore (_).

Hereditary

The control inherits the ClientIDMode parameter of the NamingContainer control. The default ClientIDMode value for the page is predictable. The default value for ClientIDMode for the Inherit control. Because the controls use Inherit by default, the default generation mode is predictable. (However, if you use Visual Studio to convert a web project to ASP.NET 4 from an earlier version, Visual Studio automatically sets the default site AutoID to a Web.config file.)

+10
source

I added ClientIDMode="Static" to the main page.

 <%@ Master ClientIDMode="Static".... 

Here, the ID will look like this: only lnkDistributors , and not as ContentPlaceHolder1_lnkDistributors or ctl00_ContentPlaceHolder1_lnkDistributors.

Now it works fine.

+1
source

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


All Articles