<%@ 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")).[color=Red]ClientID[/color]%>')" />
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>说明
[1].例如在用户控件中使用的控件,在DataList的ItemTemplate模板中使用的控件等等,.NET都会按一定的规律生成其客户端ID;
评论(0)
暂无评论。