ID vs UniqueID vs ClientID in webform
What is difference between UniqueID and ClientID in ASP.Net Controls ?
Control.ClientID
ClientID will be a unique ID string that is rendered to the client to identify the control in the output HTML.
It uses _ to include parent controls (container) ID to make it unique.
For example, ct100_ContentPlaceHolder1_txtLogin. The client id for control will be generated by appending the parent container's(parent control) ID with the original control id with "_" as delimeter.
In our example, ContentPlaceHolder1 is the ContentPlaceHolder ID of the master page which is the parent container for txtLogin.
To get the ClientID of a control,
txtLogin.ClientID
The client id can be used to identify the control in client side scripting like javascript.
Control.UniqueID
UniqueID
is also a uniquely identifiable ID for a control but only used by
ASP.Net page framework to identify the control.
It uses $ to include parent controls (container) ID to make it unique.
For example, ct100$ContentPlaceHolder1$txtLogin. Similar to ClientID, UniqueID is also generated by appending the parent container's(parent control) ID with the original control id but with $ delimeter.
To get the UniqueID of a control,
txtLogin.UniqueID
The unique id is used by the Asp.Net framework to identify the control when the page is posted back to the server.
Why ClientID and UniqueID
Well, as you probably know UniqueID is used with name attribute and ClientId with id attribute of rendered HTML tag.
UniqueID uses colon as separator. On the other hand ClientId uses underscore as separator, because colon is not allowed in JavaScript variable names.
ClientID is indeed also unique on the Page as UniqueID is, but ClientID is targeted at client-side processing and UniqueID for server-side (pretty obvious),the latter especially to route for postback data and events with composite controls
However I think some reasoning might be that using underscore as separator in normal Control IDs is pretty common behavior and therefore underscore cannot be used in UniqueID as control separator (if we'd theoretically think managing with one property),because you couldn't make distinction between controls.
On the other hand for the same reasoning, you can't use colon in Control IDs, Page Framework does not allow it, so that it makes sure colons can't get to the ClientIDs (this was because of JavaScript does not like it).
And for these reasons, colon is pretty good choice to be used in UniqueID, because FindControl method can use it to navigate Control tree and locate controls (it can easily split the UniqueID).
Difference between ID and control.ClientID OR why use control.ClientID if I can access control through ID
问题
This is the code from .aspx file
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Again</title>
<script type="text/javascript">
function Validate() {
if (document.getElementById("txtLogin").value == "") {
alert("Enter login name.");
}
if (document.getElementById("<%=txtLogin.ClientID%>").value == "") {
alert("Enter login name.");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtLogin" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Login" OnClientClick="Validate()" />
</form>
</body>
</html>
-
In function Validate() I can access textbox using Id of control i.e.;
getElementById("txtLogin")
so should I use the second approach which is accessing control throughcontrol.ClientID
and why? -
My first understanding was that to access server control I have to use this syntax
<%= %>
but now I come to know from this example that I can access server side control simply throughgetElementById("ID-of-control")
.
答案
The ID generated in the final HTML is not guaranteed to remain the same as the one in your aspx source.
When you'll put the controls inside naming containers the ID will have prepended one or several parent ids to ensure its uniqueness.
The ClientId property will always give you the final form of the ID attribute as it ends up in the HTML so it's always recommended to use that in your javascript.
ID vs UniqueID vs ClientID vs UniqueClientID vs StaticClientID?
- The ID is the serverside ID that you use in your code.
- The UniqueId corresponds to the "name" attribute of the generated HTML element.
- The ClientID corresponds to the "id" attribute of the generated html element. So it depends which attribute you need (name is sent with form post, id is used for DOM manipulation).
- Not sure what the uniqueclientid is :)
ASP.Net 4 adds clientIdMode which allows you to force the id attribute to match the serverside id (and thus be more predictable) if you set it to "static".
ASP.Net: ClientID not correct in code-behind of a user control
答案1
You should try moving the code that adds the onclick attribute to the button in the PreRender event (or OnPreRender override) in your page or user-control.
That should probably get the ClientID right.
答案2
I don't know which asp.net version you are using but in 4.0 you can declare inside any server control ClientIDMode="static" and it will give you the exact id in browser.
Example:
<asp:Textbox id="txtName" runat="server" ClientIdMode="static"/>
Others are predictable, inherit and it can be used with ClientIdRowsuffix.Can be used at page level and even on master pages and even in web.config file.
Example on web.config file:
<system.web>
<Pages clientIDMode="predictable"/>
other system web properties
</system.web>
Watched Craig shoemaker's Video at tekpub, you can also read more about it at Rick's bloglink text. It's pretty cool tho.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-04-10 设置断点后,查看堆栈信息,判断错误调用的来源是哪里