The main idea is to use ControlAdapter that will care about ClientIDs.
You need four steps to make it work.

Demo: http://show-demos.net/NoClientId

Step 1: Download Project source files.
Its just two files: NoIdAdapter.cs and NoIdAdapter.browser

Step 2: Add specialized ASP.NET folder named "App_Browsers".
To do this, right click on your Project in Solution Explorer and add it:

App_Browsers folder

Step 3: Add file NoIdAdapter.cs to your project.

Step 4: Place file NoIdAdapter.browser into "App_Browsers" folder and include it to Project.

That's it!

Now you have special attribute "noid" that you can add to controls. If you set it to "true" or "True" - ClientID will be not rendered for this control.

IMPORTANT:

  • Attribute Name and its values are case-sensitive - this was done for maximal performance. ControlAdapter will check for this attribute for almost all controls on the Page. So this check must be very fast.
  • ContolAdapter handles only controls that not support interfaces IPostBackDataHandler and IPostBackEventHandler. Exception was made only for Table control - it implements IPostBackEventHandler interface but attribute "noid" will work for it.

You can use this attribute declarative in binding (assuming we have protected bool field named "NoIds"):

<asp:Repeater ID="repeater" runat="server">
    <ItemTemplate>
        <asp:Panel ID="mainPanel" runat="server" noid='<%# NoIds %>' CssClass="panel">
            <asp:Panel ID="childPanel" runat="server" noid='<%# NoIds %>'>
                <div id="oneMorePanel" runat="server" noid='<%# NoIds %>'>
                    <asp:Label ID="someLabel" runat="server" noid='<%# NoIds %>' Text="Top Label"/><br />
                    <asp:HyperLink ID="hyperLink" runat="server" noid='<%# NoIds %>' NavigateUrl="http://asp.net" >
                        <asp:Image ID="someImage" runat="server" noid='<%# NoIds %>' ImageUrl="/img/img.png" /><br />
                        <asp:Table ID="table" runat="server" noid='<%# NoIds %>'>
                            <asp:TableHeaderRow ID="headerRow" runat="server" noid='<%# NoIds %>'>
                                <asp:TableHeaderCell ID="headerCell" runat="server" noid='<%# NoIds %>'>
                                    <asp:Label ID="label" runat="server" noid='<%# NoIds %>'>Test</asp:Label>
                                </asp:TableHeaderCell>
                            </asp:TableHeaderRow>
                        </asp:Table>
                    </asp:HyperLink>
                </div>
            </asp:Panel>
        </asp:Panel>
    </ItemTemplate>
</asp:Repeater>

Or set it from CodeBehind (assuming we have protected bool field named "NoIds"):

someControl.Attributes.Add("noid", NoIds.ToString());

Last edited Jun 8, 2011 at 2:44 PM by Lisetsky_Val, version 4

Comments

No comments yet.