ID,ClientID和UniqueID - .net语言 -

ID,ClientID和UniqueID

时间:2010-01-27 00:02:11   来源:   评论:加载中...   点击:加载中...
在ASP.NET 的服务器端控件中有三种关于 ID 的属性,即 ID, ClientID 和 UniqueID。ID 表示控件的服务器端编程的标识符,我们写"服...

}

这个方法得到hidden input的值并返回,这样我们就得到了ID的前缀。第二个方法用来查询空间

function readValue(ctrlName)
{      
var prefix = getCrtlPrefix();          
var        objCrtl = document.getElementById(prefix + ctrlName);
           if (objCrtl)                 alert ( "Prefix: " + prefix + " - value: " + objCrtl.value);
           else                 alert("not found!");
}
这个方法显示textbox控件的值。你会注意到,这里调用了getCtrlPrefix来计算textbox控件的ClientID。 我们可以增加一个按钮来调用这个方法:

<input type=button value="Read Value" onclick="javascript:readValue('mytext')">
这个html button会调用readValue。最后,把这个javascript的js文件加入aspx中。

<script language="JavaScript" src="mycontrol.js"></script>
运行这个页面,在text box中输入写数据,然后点击按钮,会出现一个消息对话框来显示ID前缀以及text box中的数据。

The difference between ID, ClientID and UniqueID

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

ID
The ID property is used to assign an identifier to an ASP.NET server control which can be later used to access that control. You can use either the field generated in the codebehind or pass the value of the ID property to the FindControl method. There is a catch though - the ID property is unique only within the current NamingContainer (page, user control, control with item template etc). If a server control is defined inside the item template of some other control (Repeater, DataGrid) or user control, its ID property is no longer unique. For example, you can add some user control twice in the same page. Any child controls of that user control will have the same ID. Also the ASP.NET page parser won't generate a codebehind field corresponding to the control ID in case the control is defined inside a template. This is the reason you cannot easily find a specific control when it is part of a template - you need to use the FindControl method of its container control instead. 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 you in the form of "_ctl0", "_ctl1", etc.

UniqueID
The UniqueID property is the page-wide unique identifier of an ASP.NET server control. Its uniqueness is guaranteed by prefixing the ID of a server control with the ID of its NamingContainer. If the NamingContainer is the Page the UniqueID will remain the same as the ID.

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

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

IPostBackEventHandler postBackInitiator =



相关热词搜索:

 
上一篇:DataGrid的自动编号问题
下一篇:C#中COOKIES的实现存取
收藏 将此文推荐给朋友
分享到: