Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is the ctl00 prefix added?

Tags:

html

asp.net

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?

like image 393
omer schleifer Avatar asked Dec 11 '22 07:12

omer schleifer


2 Answers

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>
like image 94
Rafael Schuch Avatar answered Jan 03 '23 06:01

Rafael Schuch


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>
like image 44
Esko Avatar answered Jan 03 '23 06:01

Esko