Master Pages and JavaScript document.getElementById
2010-09-20 10:55
447 查看
Whenever a control is inside a Master Page, the client ID of the
control would get appended with its content placeholder ID. So, for an
element with an ID “
”, the new client ID would look something like “
”.
So, when you try to use
, you will not get access to the
textbox in JavaScript. You need to access it by calling
.
To avoid hard coding of very long client IDs, we can access the control by using
. This will give us access to
.
Now, this will work fine until and unless the script is inline with the
ASPX page, i.e., if the script is included as part of the ASPX page.
But, the same won’t work if you have the script in a separate .js
file and add it to the ASPX page by specifying its location.
So, in this scenario, to get access to the control, we have to hard
code the control’s ID. But hard coding is not the ideal way of coding.
To avoid this situation, what we can do is maintain a mapping between
the client ID and the server ID. In the JavaScript code, we can get the
client ID of the control by giving its server ID. This can be achieved
as shown below.
control would get appended with its content placeholder ID. So, for an
element with an ID “
txtTest
”, the new client ID would look something like “
ctl00_ContentPlaceHolder1_ txtTest
”.
So, when you try to use
document.getElementById(‘txtTest’)
, you will not get access to the
txtTest
textbox in JavaScript. You need to access it by calling
document.getElementById(‘ctl00_ContentPlaceHolder1_ txtTest’)
.
To avoid hard coding of very long client IDs, we can access the control by using
document.getElementById(' <%=txtTest.ClientID%>' )
. This will give us access to
txtTest
.
Now, this will work fine until and unless the script is inline with the
ASPX page, i.e., if the script is included as part of the ASPX page.
But, the same won’t work if you have the script in a separate .js
file and add it to the ASPX page by specifying its location.
So, in this scenario, to get access to the control, we have to hard
code the control’s ID. But hard coding is not the ideal way of coding.
To avoid this situation, what we can do is maintain a mapping between
the client ID and the server ID. In the JavaScript code, we can get the
client ID of the control by giving its server ID. This can be achieved
as shown below.
相关文章推荐
- Master Pages and JavaScript document.getElementById
- javascript 如何正确使用getElementById,getElementsByName(), and getElementsByTagName()
- javascript-1.document.getElementById
- 【JavaScript】document.getElementById方法在Firefox与IE中的区别
- javascript:document.getElementById('<%=this.txtCreateTimeEnd.ClientID %>').value
- javascript typeof id==='string'?document.getElementById(id):id解释[原创]_基础知识_脚本之家
- 【javascript基础】document.getElementById
- javaScript中document.getElementById 你了解多少?
- javascript在html中的加载顺序------为什么window.alert(document.getElementById("time").innerTex)无法弹出对话框
- 【转】document.getElementById与getElementsByName的区别(JavaScript)
- javascript里Dom问题之document.getElementById('xx').innerHTML
- javascript用$代替document.getElementById()方法
- document.getElementById与getElementsByName的区别(JavaScript)
- javascript:使用document.getElementById读取数据为空分析
- (转)Javascript如何正确使用getElementById,getElementsByName() and getElementsByTagName()
- javascript代码在ie8里报错 document.getElementById(...) 为空或不是对象的解决方法 原创
- Javascript 常用方法:document.getElementById(" ")
- 在JavaScript中使用document.getElementById()获取对象失败的原因
- JavaScript中document.getElementById和document.write