10.7. ClientIDMode

A long-term irritation in ASP.NET is the lack of control you have over the ID property of rendered controls. For example, take the following HTML that is rendered from a few simple controls that are nested inside a master page:

<span id="MainContent_label1"></span>
<div id="MainContent_panel1">
  <span id="MainContent_label2"></span>
</div>

Most of the time, ASP.NET's automatic ID generation features work pretty well, but in some situations, say, when working with master pages or writing client script, you need a finer level of control. ASP.NET 4.0 gives you this control with the new ClientIDMode.

ClientIDMode has four settings:

  • AutoID: Works as per previous ASP.NET releases.

  • Static: Allows you to specify the ID that is used. Warning: you can obviously generate duplicate client IDs, so it is up to you to ensure your ID is unique, or you'll face client-side script hell (well, probably an annoying JavaScript error, anyway).

  • Predictable: Used in conjunction with RowClientIdSuffix property to generate incrementing IDs for repeating controls such as DataGrid and Repeater, for example, myrow1, myrow2, myrow3.

  • Inherit: Uses the same ClientIDMode as its parent control (default).

ClientIdMode can be set at the control, page, and application levels:

  • To set on an individual control:

    <asp:Label ID="lblTest" runat="server" Text="Test" ClientIdMode="Inherit"></asp:Label>

  • To set at the page level:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" ClientIdMode="Predictable" %>

  • To set application-wide in Web.config:

    <system.web>
      <pages clientIdMode="Inherit"></pages>
    </system.web>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.123.103