日期:2014-05-18 浏览次数:20493 次
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListDemo.aspx.cs" Inherits="DataListDemo" %>
<!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 type="text/javascript">
function showHide(lbl)
{
if(document.getElementById(lbl).style.display == "none")
{
document.getElementById(lbl).style.display = "inline";
}
else
{
document.getElementById(lbl).style.display = "none"
}
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select * from products"></asp:SqlDataSource>
</form>
</body>
</html>
------解决方案--------------------
使用DataList的ItemDataBound事件循环的为每一个Button添加事件
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
(e.Item.FindControl("Button1") as Button).Attributes.Add("onclick", "show('" + 当前的行的Label-- > id + "')");
}