Accessing the control’s id at runtime, when the page being used is the master page, has been a persistent issue, especially when we are trying to access the control’s id(Client Id) using Javascript. When we are adding the content to our aspx page, it resembles : <asp:Content ID=”Content1″ ContentPlaceHolderID=”MindfireContent” Runat=”Server”> At runtime time or after Page Render the textbox will render as <input id=”ctl00_MindfireContent_txtMindfire”/> Common Approach which we all are following to get the Control’s id (Client Id of the control) on Javascript function When we trying to access the control’s id in our javascript function, We are following two approaches. 1. Calling javascript from the server side and passing control’s client id as parameter Both of the approaches are not good practice if you will consider the performance and standard of your application but we dont have any other way.So we have one property named as ClientIDMode on DotNet Framework 4.0.By using this property, the user can decide what should be the control id at runtime or after the page renders. There are four types of ClientIDMode 1. AutoID Note: Given below are few examples for each type of ClientIDMode which shows how to access the Control’s id (Client id) through Javascript function and how the Control renders for each type.
|
1. AutoID This is the existing behavior as in ASP.NET 1.x-3.x.
|
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> <script type="text/javascript" language="javascript"> function ClientIdExample() { var id = document.getElementById('<%=txtMinfire.ClientID%>'); alert(id); } window.onload = ClientIdExample </script> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <div id="divMindfire"> <asp:TextBox ID="txtMinfire" ClientIDMode="AutoID" Text="Proud to be Mindfireans" runat="server" /> </div> </asp:Content> After Page Renders <input name="ctl00$MainContent$txtMinfire" type="text" value="Proud to be Mindfireans" id="ctl00_MainContent_txtMinfire" />
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> <script type="text/javascript" language="javascript"> function ClientIdExample() { var id = document.getElementById('txtMinfire'); alert(id); } window.onload = ClientIdExample </script> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <div id="divMindfire"> <asp:TextBox ID="txtMinfire" ClientIDMode="Static" Text="Proud to be Mindfireans" runat="server" /> </div> </asp:Content>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> <script type="text/javascript" language="javascript"> function ClientIdExample() { var id = document.getElementById('MainContent_txtMinfire'); // ContentPlaceHolderID_IDOfTheControl alert(id); } window.onload = ClientIdExample </script> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <div id="divMindfire"> <asp:TextBox ID="txtMinfire" ClientIDMode="Inherit" Text="Proud to be Mindfireans" runat="server" /> </div> </asp:Content>
<asp:GridView ID="gvMindfire" runat="server" AutoGenerateColumns="false" ClientIDMode="Predictable" > <Columns> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Company Name"> <ItemTemplate> <asp:Label ID="lblOrganisation" runat="server" Text='<%# Eval("CompName") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
<table id="gvMindfire" style="border-collapse: collapse" cellspacing="0" rules="all" border="1"> <tbody> <tr> <th scope="col">ID</th> <th scope="col">Name</th> <th scope="col">Company Name</th> </tr> <tr> <td><span id="gvMindfire_lblID_0">001</span></td> <td><span id="gvMindfire_lblName_0">Srikanta</span></td> <td><span id="gvMindfire_lblOrganisation_0">Mindfire</span></td> </tr> <tr> <td><span id="gvMindfire_lblID_1">007</span></td> <td><span id="gvMindfire_lblName_1">Kaushik</span></td> <td><span id="gvMindfire_lblOrganisation_1">Mindfire</span></td> </tr> ......... ......... </tbody> </table>
Output with ClientIDRowSuffix
<table id="gvMindfire" style="border-collapse: collapse" cellspacing="0" rules="all" border="1"> <tbody> <tr> <th scope="col">ID</th> <th scope="col">Name</th> <th scope="col">Company Name</th> </tr> <tr> <td><span id="gvMindfire_lblID_001">001</span></td> <td><span id="gvMindfire_lblName_001">Srikanta</span></td> <td><span id="gvMindfire_lblOrganisation_001">Mindfire</span></td> </tr> <tr> <td><span id="gvMindfire_lblID_007">007</span></td> <td><span id="gvMindfire_lblName_007">Kaushik</span></td> <td><span id="gvMindfire_lblOrganisation_007">Mindfire</span></td> </tr> ...... </tbody> </table>
3.Control Level
Setting ClientIDMode at Page Level
<%@ Page Language=”C#” ClientIDMode =”Inherit” AutoEventWireup=”true” CodeBehind=”Mindfire.aspx.cs” Inherits=”Mindfire” %>
Setting ClientIDMode at Control Level
Each and every server control in ASP.NET 4.0 has this property and the default value is inherit.