外部JS得到客户端ID
2010-05-23 11:30
134 查看
大家都知道ASP.NET为了区分控件 HTML标签客户端ID与服务器ID在某些情况下是不同的
例如 使用了模板页
假如有个服务器TextBox ID为txtName
那么客户端ID可能会是ctl00_ContentPlaceHolder1_txtName
当写JScript代码时,想得到某控件
如果JScript代码直接写在本页时 可以用如下代码获得:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function GetValue(){
var txtNameValue=$id("<%=txtName.ClientID %>").value;
alert(txtNameValue);
}
function $id(id) {
return document.getElementById(id);
}
</script>
</head>
<body>
<form id="form1" runat="server" style="">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<input type="button" value="查看" onclick="GetValue()" />
</form>
</body>
</html>
当JScript写在单独的Js文件中 这种方法就不行了
可以用如下方式解决
首先在页面定义一个函数(这个函数相当于OOP中的类)
例如:
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" style="">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<input type="button" value="查看" onclick="GetValue()" />
</form>
<script type="text/javascript">
function IdList()
{
this.txtShopName="<%= txtName.ClientID %>";
//
}
</script>
<script src="js/test.js" language="javascript" type="text/javascript" ></script>
</body>
</html>
外部JScript文件
然后在JScript文件中实例化这个“类” 例如:
test.js
//关联Default2.aspx
function GetValue(){
var idList=new IdList();
alert($id(idList.txtShopName).value);
}
//简化document.getElementById
function $id(id) {
return document.getElementById(id);
}
如上方法就可解决外部JScript不能得到正确的客户端ID了
为了便于理解 最好在JScript文件中标注 与那个页面关联
http://www.e5-zone.com/shopRegister.html 该页面使用了此方法 提供出来 以便大家参考
例如 使用了模板页
假如有个服务器TextBox ID为txtName
那么客户端ID可能会是ctl00_ContentPlaceHolder1_txtName
当写JScript代码时,想得到某控件
如果JScript代码直接写在本页时 可以用如下代码获得:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function GetValue(){
var txtNameValue=$id("<%=txtName.ClientID %>").value;
alert(txtNameValue);
}
function $id(id) {
return document.getElementById(id);
}
</script>
</head>
<body>
<form id="form1" runat="server" style="">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<input type="button" value="查看" onclick="GetValue()" />
</form>
</body>
</html>
当JScript写在单独的Js文件中 这种方法就不行了
可以用如下方式解决
首先在页面定义一个函数(这个函数相当于OOP中的类)
例如:
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" style="">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<input type="button" value="查看" onclick="GetValue()" />
</form>
<script type="text/javascript">
function IdList()
{
this.txtShopName="<%= txtName.ClientID %>";
//
}
</script>
<script src="js/test.js" language="javascript" type="text/javascript" ></script>
</body>
</html>
外部JScript文件
然后在JScript文件中实例化这个“类” 例如:
test.js
//关联Default2.aspx
function GetValue(){
var idList=new IdList();
alert($id(idList.txtShopName).value);
}
//简化document.getElementById
function $id(id) {
return document.getElementById(id);
}
如上方法就可解决外部JScript不能得到正确的客户端ID了
为了便于理解 最好在JScript文件中标注 与那个页面关联
http://www.e5-zone.com/shopRegister.html 该页面使用了此方法 提供出来 以便大家参考
相关文章推荐
- 外部JS得到客户端ID
- 在使用母版页时,在前台js中得到子页面中的控件的id
- 怎样用JS获取ASP.NET服务器控件的客户端ID
- js获取客户端时区ID
- asp页面上用js获取服务器控件客户端id的办法
- 如何用C#及JS得到服务器端和客户端的IP
- 如何用C#及JS得到服务器端和客户端的IP
- [正则表达式]前台JS得到控件ID (该控件被其它控件包住了)
- Node.js 使用http客户端得到网站的图片下载到本地
- js中得到checkbox标签选中的信息的id
- 在js中判断checkboxlist(.net控件客户端id)是否有选中
- 关于//注册客户端id ,以及注册js文件到page里
- [原创]用户控件中使用Js,如何在客户端引用用户控件中某个控件的ID
- 在用户控件中撰写JS时控件的客户端ID问题
- js获取服务器生成并返回客户端呈现给客户的控件id的方法
- js : 在使用母版页时,在前台js中得到子页面中的控件的id
- 用js得到网页中所有的div的id
- js回车事件及得到光标所在元素id
- 关于js获取不到客户端控件id的问题【搜藏】