不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
使用ClientID获取服务器端控件在客户端的ID
编辑:dnawo 日期:2010-04-20
在ASP.NET中,在某些情况下,对于一些服务器控件[1],.NET会在客户端按一定的规律生成其客户端ID,这就造成控件的客户端ID和服务器端ID不一样,此时若要在客户端脚本中使用该控件,就必需先知道其客户端ID。幸好.NET为每个控件都提供了一个ClientID属性,该属性值即为控件在客户端的ID,下边是一个使用示例:
说明
[1].例如在用户控件中使用的控件,在DataList的ItemTemplate模板中使用的控件等等,.NET都会按一定的规律生成其客户端ID;
复制内容到剪贴板
程序代码

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection conn = new SqlConnection("server=(local);database=Northwind;user id=sa;password=sa;"))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("Select TOP 10 * FROM Products", conn))
{
DataTable table = new DataTable();
adapter.Fill(table);
DataList1.DataSource = table;
DataList1.DataBind();
}
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ClientID示例-Mzwu.Com</title>
<script type="text/javascript">
function $(id){ return document.getElementById(id); }
function Show(id){ alert($(id).innerHTML); }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
<input type="button" value="button" onclick="Show('<%# ((Label)Container.FindControl("Label1")).ClientID%>')" />
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection conn = new SqlConnection("server=(local);database=Northwind;user id=sa;password=sa;"))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("Select TOP 10 * FROM Products", conn))
{
DataTable table = new DataTable();
adapter.Fill(table);
DataList1.DataSource = table;
DataList1.DataBind();
}
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ClientID示例-Mzwu.Com</title>
<script type="text/javascript">
function $(id){ return document.getElementById(id); }
function Show(id){ alert($(id).innerHTML); }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
<input type="button" value="button" onclick="Show('<%# ((Label)Container.FindControl("Label1")).ClientID%>')" />
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
说明
[1].例如在用户控件中使用的控件,在DataList的ItemTemplate模板中使用的控件等等,.NET都会按一定的规律生成其客户端ID;






评论: 0 | 引用: 0 | 查看次数: 4734
发表评论
请登录后再发表评论!