ID,ClientIDandUniqueID的区别 jquery uniqueid

转载:http://hi.baidu.com/tlq_1983/blog/item/ab8f4bfc30188ff5fd037f18.html

The difference between ID, ClientID and UniqueID

I this post I will try to explain thedifference between those three commonly used properties. Eachproperty is described in a separate section. Attached you can finda sample web site as well as two screenshots visually depicting thedifference between the ID, ClientID and UniqueIDproperties.

ID
The ID property is used to assign an identifier to an ASP.NETserver control which can be later used to access that control. Youcan use either the field generated in the codebehind or pass thevalue of the ID property to the FindControl method. There is acatch though - the ID property is unique only within the currentNamingContainer (page, user control, control with item templateetc). If a server control is defined inside the item template ofsome other control (Repeater, DataGrid) or user control, its IDproperty is no longer unique. For example, you can add some usercontrol twice in the same page. Any child controls of that usercontrol will have the same ID. Also the ASP.NET page parser won'tgenerate a codebehind field corresponding to the control ID in casethe control is defined inside a template. This is the reason youcannot easily find a specific control when it is part of a template- you need to use the FindControl method of its container controlinstead. As a side note, setting the ID property is not mandatory.If you don't set it the ASP.NET Runtime will generate one for youin the form of "_ctl0", "_ctl1", etc.

UniqueID
The UniqueID property is the page-wide unique identifier of anASP.NET server control. Its uniqueness is guaranteed by prefixingthe ID of a server control with the ID of its NamingContainer. Ifthe NamingContainer is the Page the UniqueID will remain the sameas the ID.

For example if a Label with ID="Label1"is defined in a user control with ID = "UserControl1" the UniqueIDof the Label will be "UserControl1$Label1". Adding another instanceof the same user control (with ID = "UserControl2") will make theUniqueID of its child label to be "UserControl2$Label1".

The UniqueID propertyis also used to provide value for the HTML "name" attribute ofinput fields (checkboxes, dropdown lists, and hidden fields).UniqueID also plays major role in postbacks. The UniqueID propertyof a server control, which supports postbacks, provides data forthe __EVENTTARGET hidden field. The ASP.NET Runtime then uses the__EVENTTARGET field to find the control which triggered thepostback and then calls its RaisePostBackEvent method. Here is somecode which illustrates the idea:

IPostBackEventHandlerpostBackInitiator =

Page.FindControl(Request.Form["__EVENTTARGET") AsIPostBackEventHandler;

if (postBackInitiator!= null)
postBackInitiator.RaisePostBackEvent(Request.Form["__EVENTARGUMENT"]);

You can use theUniqueID property to find any control no matter how deep it isnested in the control hierarchy. Just pass its value to theFindControl method of the Page.

ClientID
The ClientID property is quite similar to the UniqueID. It isgenerated following the same rules (the ID of the control prefixedby the ID of its NamingContainer). The only difference is theseparator - for the ClientID it is the "_" (underscore)symbol.
The ClientID property is globally unique amongall controls defined in an ASP.NET page. You may ask why we needtwo different globally unique properties. The answer is thatClientID serves a different purpose than UniqueID. In most servercontrols the ClientID property provides the value for the HTML "id"attribute of the HTML tag of that server control. For examplethis:

<asp:Label ID="Label1"Text="Label" />

will render as this:

<spanid="Label1">Label</span>

That's why you often use the followingJavaScript statement to access the DOM element corresponding tosome ASP.NET server control:

var label =document.getElementByIdx("<%=Label1.ClientID%>");

which in turn renders as:

var label =document.getElementByIdx("Label1");

It is worth mentioning that the valuesof the ID, UniqueID and ClientID will be the same if the control isdefined in the master page (or the page). This however can oftenlead to unexpected errors. If the ID of the control is hardcodedinside the JavaScript statement (e.g. "Label1") this code will onlywork provided the control is defined in the Page or master page.Moving the control and the JavaScript code into a userc controlwith ID "UserControl1" will fail at runtime because the controlwill now render as:
ID,ClientIDandUniqueID的区别 jquery uniqueid
<spanid="UserControl1_Label1">Label</span>


That's why you should prefer using the"<%= Label1.ClientID%>" syntax to getthe client-side id of server controls.


Additionaly the ClientID is used inASP.NET Ajax as the unique identifier of client-side controls. Thusthe following JavaScript statement is commonly used:

var control = $find("<%=MyControl1.ClientID %>");

  

爱华网本文地址 » http://www.413yy.cn/a/25101016/323211.html

更多阅读

红米1S电信版和移动版和联通版的区别 红米1s移动版破解联通

红米1S电信版和移动版和联通版的区别——简介 直到今天,发烧级平民手机红米手机各种移动运营商制式总算全部发布起了!这也给不是IT行业的朋友选购带来了纠结,哪一版本适合自己呢?各个版本有什么全部呢?今天小编为你一一解析!希望能给你提

大理石与花岗岩的区别 大理石有辐射吗

大理石与花岗岩的区别 ? ?一、按石材成因划分:1.大理石大理石属于变质岩变质岩是在高温高压和矿物质的混合作用下由一种石头自然变质成的另一种石头。质变可能是重结晶、纹理改变或颜色改变。天然大理石是地壳中原有

石英表和机械表的区别 精 机械表石英表区别

石英表和机械表的区别 精——简介准备想购买一块手表,但不很清楚石英表和机械表的区别在哪,经过一番购买咨询,也知道了他们之间的一些区别,下面分享给大家,在买手表、钟表等时可以参考一下哦。石英表和机械表的区别 精——方法/步骤石英

电脑待机与休眠的区别 电脑待机和休眠的区别

电脑待机与休眠的区别? ? 首先确定的是休眠和待机不能下载的。因为硬盘都断电了。?休眠和待机的原理都是吧内存的东西保存住,下次就可以不用重新加载操作系统,但他们最大的不同是前者把内存中的数据临时保存到硬盘中,下次开机就直接

声明:《ID,ClientIDandUniqueID的区别 jquery uniqueid》为网友孤天狼分享!如侵犯到您的合法权益请联系我们删除