简体   繁体   中英

ASP.Net - Naming Containers and ClientIDMode=“Static”

MSDN documentation states with regard to the ClientIdMode:

Static The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.

source: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode%28v=vs.100%29.aspx

However, i am not finding the "top of the hierarchy" business to be the case. For example, I have a usercontrol:

<uc1:WidgetsListControl runat="server" id="WidgetsListControl" ClientIDMode="Static"  />

For good measure, I set the clientidmode in the control source as well although I'm not sure which one is needed:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WidgetsListControl.ascx.cs" Inherits="WebApplication1.WidgetsListControl" EnableViewState="false" ClientIDMode="Static" %>

Within the user control, I have a textbox:

<asp:TextBox ID="testTextBox" runat="server" />

My expectation is that the text box would be named something like WidgetsListControl$testTextBox

However what I find upon view source is:

<input name="ctl00$MainContent$WidgetsListControl$testTextBox" id="testTextBox" type="text"/>

What am I missing? Is there a way to achieve what I'm looking for (shorter ids) without setting 'static' on every control within the user control?

EDIT:

Actually after looking closer, I am finding that the ID attribute is working as described in the MSDN - however the name attribute is still the full concatenation of the naming-container hierarchy.

Given a site of high complexity, the names and IDs of these controls start to take up the majority of the bandwidth (markup size). I can't seem to find any good workaround to slim down the markup in this regard.

You must have this control within a Panel or something with runat="server" and the custom control itself is a naming container for its children. If you want identifiers to be how you name them with no change, you'll want to use the Predictable (IIRC) client id mode.

Note: though looking at the documentation for Predictable this doesn't seem to be the case, I'm sure it's what I've always used to get 'clean' .NET identifiers in ASP.NET.

Further, if you want to apply the ClientIDMode globally, then specify it in the web.config file in the attribute of the <pages> element.

ClientIdMode="Static" does work as described by Microsoft.

Make sure your looking at the id in the generated source, not the name. That's the mistake I made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM