Take Control Of Web Control ClientID Values in ASP.NET 4
2011-12-11 22:49
537 查看
http://www.4guysfromrolla.com/articles/031710-1.aspx
Web controls in ASP.NET also have
could be literally translated into the client-side
the ASP.NET engine may be composing the resulting web page from a multitude of sources, including: a master page (
a Web control in the master page could have the same
for the entire web page.
The way this is accomplished is by translating the server-side
Set the client-side
Inspect the Web control's parent controls.
For each naming container encountered, prefix the client-side
Certain types of Web controls are classified as naming containers. For example, master pages and ContentPlaceHolder controls are both naming
containers. To see the effect naming containers have in altering the client-side
page and a content page. Add a TextBox to the content page and set its server-side
Next, visit the page through a browser and do a View/Source. You'll see that the TextBox control's rendered client-side
like
content page (named
...
Most Web controls that display data or use templates operate as naming containers, as well. To understand why, consider a GridView that
contains a TemplateField with a Label named
happen if the GridView and its rows were not a naming container - the four Label controls would end up having the same
in rendered
...
You can tell that the GridView is a naming container because its ID - GridViewID - appears in the rendered client-side
are also naming containers. They appear in the client-side
Retrieving the Client-Side
sample from envykok
Taking Control Of The Rendered Client-Side
ASP.NET 4.0 grants page developers finer control over how a Web control renders its client-side
via a new property,
The
meaning that it applies to all Web controls in the ASP.NET Toolbox, including the
The
the client-side
used for controls with repeating templates, like the GridView or ListView. When selected, the client-side
to the generated client-side
...
Web controls in ASP.NET also have
IDvalues that uniquely identify each Web control. At first blush it may seem that the
IDproperty values of an ASP.NET Web control
could be literally translated into the client-side
idvalue, since both must be unique. But keep in mind that several ASP.NET files may be used in composing a single web page. For instance, when a browser requests the web page
Default.aspx,
the ASP.NET engine may be composing the resulting web page from a multitude of sources, including: a master page (
Site.master); a content page (
Default.aspx); and, perhaps, one or more User Controls. It is certainly possible that
a Web control in the master page could have the same
IDvalue as a Web control in the content page, or in a User Control. Consequently, ASP.NET needs some mechanism to ensure that the client-side
idvalues it generates are unique
for the entire web page.
The way this is accomplished is by translating the server-side
IDvalue into a client-side
idvalue using the following sequence of steps:
Set the client-side
idvalue to the name of the Web control's server-side
IDvalue.
Inspect the Web control's parent controls.
For each naming container encountered, prefix the client-side
idvalue with the
IDproperty of the naming container.
Certain types of Web controls are classified as naming containers. For example, master pages and ContentPlaceHolder controls are both naming
containers. To see the effect naming containers have in altering the client-side
idrendered, create a simple ASP.NET site with a master
page and a content page. Add a TextBox to the content page and set its server-side
IDproperty value to something simple, like
txtName.
Next, visit the page through a browser and do a View/Source. You'll see that the TextBox control's rendered client-side
idvalue is something
like
ctl00_ContentPlaceHolder1_txtName. This is because the TextBox is within two naming containers: the ContentPlaceHolder control in the
content page (named
ContentPlaceHolder1, by default) and the master page itself (named
ctl00).
...
Most Web controls that display data or use templates operate as naming containers, as well. To understand why, consider a GridView that
contains a TemplateField with a Label named
lblName. The GridView renders this template once for each record bound to it, so if four records are bound to the GridView then there will be four Label controls rendered in the page. Imagine what would
happen if the GridView and its rows were not a naming container - the four Label controls would end up having the same
idattribute value. To prevent this naming conflict the GridView and its rows are made naming containers, resulting
in rendered
idattributes like:
GridViewID_ctl00_lblName
GridViewID_ctl01_lblName
GridViewID_ctl02_lblName
...
You can tell that the GridView is a naming container because its ID - GridViewID - appears in the rendered client-side
idattribute. The rows of the GridView
are also naming containers. They appear in the client-side
idwith the auto-generated names
ctl00,
ctl01, and so on.
Retrieving the Client-Side
idValue At Runtime
sample from envykok
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestClientID._Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { var txt = document.getElementById('<%=TextBoxID.ClientID%>'); txt.focus(); }); </script> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <h2> Welcome to ASP.NET! </h2> <p> To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>. </p> <p> You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409" title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>. </p> <asp:TextBox ID="TextBoxID" runat="server"></asp:TextBox> </asp:Content>
Taking Control Of The Rendered Client-Side
idValue With ASP.NET 4.0
ASP.NET 4.0 grants page developers finer control over how a Web control renders its client-side
idvalue
via a new property,
ClientIDMode.
The
ClientIDModeproperty is defined on the
Controlclass,
meaning that it applies to all Web controls in the ASP.NET Toolbox, including the
Pageitself.
The
ClientIDModeproperty can be set to one of the following four values:
AutoID- uses the same series of steps for computing the client-side
idvalue as in previous versions of ASP.NET.
Static-
the client-side
idvalue is the same as the server-side
IDproperty value.
Predictable-
used for controls with repeating templates, like the GridView or ListView. When selected, the client-side
idvalue is concatenated with a specified
ClientIDRowSuffixproperty, which is the name of a data field whose value is appended
to the generated client-side
id.
Inherit- specifies that the control's client-side
idvalue be generated the same way as its parent.
...
相关文章推荐
- Take Advantage of ASP.NET Built-in Features to Fend Off Web Attacks
- Customize web.config sections and register your own configuration then get values in asp.net
- 用jQuery访问ASP.Net WebControl丑陋的ClientID方法
- [ASP.NET AJAX]Important notes when we create web service methods for some extenders in AjaxControlToolkit package
- Resetting Form Field Values in an ASP.NET WebForm
- Model Validation in ASP.NET Web API By Mike Wasson|July 20, 2012 268 of 294 people found this helpful
- 用jQuery访问ASP.Net WebControl的ClientID方法
- 【转】Asp.net控件开发学习笔记整理篇 - WebControl基类
- Prevent ASP.NET web.config inheritance, and inheritInChildApplications attribute -ASP.NET如何防止web.config向下(子应用程序)继承?
- Handling HTTP 404 Error in ASP.NET Web API
- Manual JSON serialization from DataReader in ASP.NET Web API
- Web Server and ASP.NET Application life Cycle in Depth [转]
- Web Server Controls->ASP.NET Literal Control
- Tracing in ASP.NET Web API
- ASP.NET自定义控件组件开发 第二章 继承WebControl的自定义控件
- How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- ASP.Net Web API 输出缓存 转载 -- Output caching in ASP.NET Web API
- How to use FtpWebRequest in asp.net
- Content Negotiation in ASP.NET Web API
- ASP.NET WebForms and MVC together in one project