Repeater控件演练
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeater.aspx.cs" Inherits="repeater" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width=400>
<asp:Repeater runat=server ID=Repeater1 DataSourceID="SqlDataSource1"
onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<td><asp:Label ID="Label1" runat=server Text='<%#Eval("ProductName")%>' ></asp:Label></td>
<td>
<asp:Button ID="ButtonOpen" runat="server" Text="Open" CommandName="Open" CommandArgument='<%#Eval("ProductID")%>' /></td>
<td></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="select top 20 * from [products]"></asp:SqlDataSource>
<asp:Button ID="ButtonSubmit" runat="server" onclick="ButtonSubmit_Click"
Text="提交" />
</div>
</form>
</body>
</html>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width=400>
<asp:Repeater runat=server ID=Repeater1 DataSourceID="SqlDataSource1"
onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<td><asp:Label ID="Label1" runat=server Text='<%#Eval("ProductName")%>' ></asp:Label></td>
<td>
<asp:Button ID="ButtonOpen" runat="server" Text="Open" CommandName="Open" CommandArgument='<%#Eval("ProductID")%>' /></td>
<td></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="select top 20 * from [products]"></asp:SqlDataSource>
<asp:Button ID="ButtonSubmit" runat="server" onclick="ButtonSubmit_Click"
Text="提交" />
</div>
</form>
</body>
</html>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)//ImgBtnPreCN
{
Control ctrl = e.Item.FindControl("ButtonOpen");
Button btn = ctrl as Button;
if (btn != null)
{
btn.Attributes.Add("onclick", "javascript:window.open('Default.aspx?ID=" + btn.CommandArgument + "');");
}
}
}
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)//ImgBtnPreCN
{
Control ctrl = e.Item.FindControl("ButtonOpen");
Button btn = ctrl as Button;
if (btn != null)
{
btn.Attributes.Add("onclick", "javascript:window.open('Default.aspx?ID=" + btn.CommandArgument + "');");
}
}
}
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.
浙公网安备 33010602011771号