Difference between ID and control.ClientID OR why use control.ClientID if I can access control through ID
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.
Difference between ID and control.ClientID OR why use control.ClientID if I can access control through ID的更多相关文章
- Method and system for implementing mandatory file access control in native discretionary access control environments
A method is provided for implementing a mandatory access control model in operating systems which na ...
- Oracle Applications Multiple Organizations Access Control for Custom Code
档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...
- Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade
XMLHttpRequest cannot load http://10.164.153.37:8050/WebService/WebService.asmx/wsGetStreetData. Res ...
- SELINUX、Security Access Control Strategy && Method And Technology Research - 安全访问控制策略及其方法技术研究
catalog . 引言 . 访问控制策略 . 访问控制方法.实现技术 . SELINUX 0. 引言 访问控制是网络安全防范和客户端安全防御的主要策略,它的主要任务是保证资源不被非法使用.保证网络/ ...
- Linux VFS Extended Attribute And Access Control Table
catalog . 简介 . 扩展属性 . 访问控制表 . 小结 0. 简介 许多文件系统都提供了一些特性,扩展了VFS层提供的标准功能,虚拟文件系统不可能为所有特性都提供具体的数据结构.超出标准的U ...
- RBAC(Role-Based Access Control,基于角色的访问控制)
RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色- ...
- 转:Oracle R12 多组织访问的控制 - MOAC(Multi-Org Access Control)
什么是MOAC MOAC(Multi-Org Access Control)为多组织访问控制,是Oracle EBS R12的重要新功能,它可以实现在一个Responsibility下对多个Opera ...
- RBAC(Role-Based Access Control)基于角色的访问控制
RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成"用 ...
- [认证授权] 6.Permission Based Access Control
在前面5篇博客中介绍了OAuth2和OIDC(OpenId Connect),其作用是授权和认证.那么当我们得到OAuth2的Access Token或者OIDC的Id Token之后,我们的资源服务 ...
随机推荐
- Linux 压缩解压缩命令详解
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- 转:安装PHP出现make: *** [sapi/cli/php] Error 1 解决办法
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_ctor':/home/king/PHP-5.2.13/ext/iconv/ ...
- Boss Bo (主席树)
主要想法:假设给你足够时间,那么就可以对每个点建议一颗线段树来查询了,但是需要将点全部按照某个特定的序列存进线段树,如代码是以树的深搜顺序作为指定顺序,这样以来我们既可以将数据查询分成诺干个区间进行查 ...
- 【2017-03-28】JS基础、DOM操作
一.JS基础 1.javascript功能 ⑴进行数据运算 ⑵控制浏览器功能 ⑶控制元素的属性.样式.内容 2.javascript位置和格式 可以放在html页的任意位置,也可以创建一个以js结尾的 ...
- 【Scala学习之二】 Scala 集合 Trait Actor
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...
- jdk和jre区别
- python seek()方法报错:“io.UnsupportedOperation: can't do nonzero cur-relative seeks”
今天使用seek()时报错了, 看下图 然后就百度了一下,找到了解决方法 这篇博客https://www.cnblogs.com/xisheng/p/7636736.html 帮忙解决了问题, 照理说 ...
- qt5.5.1 移植4412的问题过程
1.编译错误: ../WTF/wtf/unicode/wchar/UnicodeWchar.h: In function 'bool WTF::Unicode::isAlphanumeric(UCha ...
- mybatis源码解析7---MappedStatement初始化过程
上一篇我们了解到了MappedStatement类就是mapper.xml中的一个sql语句,而Configuration初始化的时候会加载所有的mapper接口类,而本篇再分析下是如何将mapper ...
- linux监控性能和网络的命令
vmstat查看机器实时的综合情况:load,内存,swap,cpu使用率等方面 procs: r:运行队列中进程数量 b:等待IO的进程数量 memory(内存): swpd:使用虚拟内存大小 fr ...