When is the ctl00 prefix added?

12,407

Solution 1

I had the same problem when I migrated from .Net 3.5 to .Net 4.0.

I solved it by adding the following configuration in web.config in IIS6. For IIS7, use the system.webServer section:

<system.web> 
  <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>

Solution 2

It's a default behavior of ASP.Net Web forms since id's in html have to be unique. Sometimes ASP.Net has to dynamically change the id according to it's parent control(s). You can read more from here http://msdn.microsoft.com/en-us/library/1d04y8ss(v=vs.100).ASPX

Usually you don't have to worry about this id. But if you need the id in client side for example to write a javascript function that needs to find the element by its id, you can use the ClientId-property to get the Id of the control:

<script type="text/javascript">
      function Test() {
          var myControl = document.getElementById('<%= myControl.ClientId %>');
      }

</script>

Solution 3

<pages **controlRenderingCompatibilityVersion="4.0"** clientIDMode="AutoID"   >

You will get 'ctl00' if you have set controlRenderingCompatibilityVersion to version "4.0" in web.config. To remove this prefix you can set this to 3.5 or earlier.

Share:
12,407

Related videos on Youtube

omer schleifer
Author by

omer schleifer

Updated on September 15, 2022

Comments

  • omer schleifer
    omer schleifer over 1 year

    I have a simple asp.net page with an asp.net link button and asp.net content tag which point to a simple asp.net master page with an asp.net content place holder and a form tag. Here is the code of these two items:

    Site.Master:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication1.SiteMaster" %>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <form runat="server">
                <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </form>
    </body>
    </html>
    

    Default.aspx:

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
    </asp:Content>
    

    For some reason when we run this simple web application on one server the id the link bhutton get is MainContent_LinkButton1 and when we run this application on another server the id the link button get is _ctl0_MainContent_LinkButton1

    Doese someone have an idia why we get the prefix ctl0 in a specific server and in another server we don't get it?

  • Liath
    Liath about 10 years
    Great answer, my personal preference is to register a JS object from the code behind holding the ClientIDs, I don't like the <%= %> syntax but each to their own!
  • omer schleifer
    omer schleifer about 10 years
    Thanks, this answer is helpful. I have already read the link you've sent. It does not explain why in some servers I get the prefix, while in other servers (same OS , same IIS , even the same DB is used) I don't get this prefix.