Repeater绑定事件
<%="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Repeart._Default" %>
<!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>Repeater绑定事件</title>
<script type="text/javascript">function SelectAll(box)
{
for(var i=0;i <document.form1.elements.length;i++)
{
var e=document.form1.elements[i];
if((e.type=='checkbox'))
{
var o=e.name.lastIndexOf('ckbIndex');
if(o!=-1)
{
e.checked=box.checked;
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting"
Width="449px">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
<br /><br />
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1" style="width:449px;border-collapse:collapse;">
<tr>
<td><input id= "chkHeader" type= "checkbox" onclick= "SelectAll(this)"/>全选</td>
<td>删除操作</td>
<td>id</td>
<td>cityid</td>
<td>city</td>
<td>father</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:CheckBox runat="server" ID="ckbIndex" Text='<%#Eval("id") %>' /></td>
<td><asp:Button ID="btn_Del"
runat="server"
Text="删除"
CommandName="del"
CommandArgument='<%#Eval("id") %>'
OnClientClick="return confirm('确定删除吗?');" />
</td>
<td><%#Eval("id") %></td>
<td><%#Eval("cityid") %></td>
<td><%#Eval("city") %></td>
<td><%#Eval("father") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnRpt" runat="server" OnClick="btnRpt_Click" Text="批量删除" OnClientClick="return confirm('确定要删除吗?')" />
</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>Repeater绑定事件</title>
<script type="text/javascript">function SelectAll(box)
{
for(var i=0;i <document.form1.elements.length;i++)
{
var e=document.form1.elements[i];
if((e.type=='checkbox'))
{
var o=e.name.lastIndexOf('ckbIndex');
if(o!=-1)
{
e.checked=box.checked;
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting"
Width="449px">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
<br /><br />
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1" style="width:449px;border-collapse:collapse;">
<tr>
<td><input id= "chkHeader" type= "checkbox" onclick= "SelectAll(this)"/>全选</td>
<td>删除操作</td>
<td>id</td>
<td>cityid</td>
<td>city</td>
<td>father</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:CheckBox runat="server" ID="ckbIndex" Text='<%#Eval("id") %>' /></td>
<td><asp:Button ID="btn_Del"
runat="server"
Text="删除"
CommandName="del"
CommandArgument='<%#Eval("id") %>'
OnClientClick="return confirm('确定删除吗?');" />
</td>
<td><%#Eval("id") %></td>
<td><%#Eval("cityid") %></td>
<td><%#Eval("city") %></td>
<td><%#Eval("father") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnRpt" runat="server" OnClick="btnRpt_Click" Text="批量删除" OnClientClick="return confirm('确定要删除吗?')" />
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Repeart
{
public partial class _Default : System.Web.UI.Page
{
string strCon = ConfigurationManager.ConnectionStrings["con"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bind();
}
}
private void bind()
{
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select top 10 * from city", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string a = GridView1.Rows[e.RowIndex].Cells[2].Text.ToString();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//string a = ((DataRowView)e.Item.DataItem)["id"].ToString();
//Response.Write(a);
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "del")
{
string a = e.CommandArgument.ToString();
Response.Write(a);
}
}
protected void btnRpt_Click(object sender, EventArgs e)
{
foreach (RepeaterItem Item in Repeater1.Items)
{
CheckBox chk = (CheckBox)Item.FindControl("ckbIndex");
if (chk.Checked)
{
//Page.ClientScript.RegisterStartupScript(GetType(), "111", "<script>alert('要批量删除的记录ID号:"+chk.Text+"');</script>");
Response.Write("要批量删除的记录ID号:"+chk.Text+"<br/>");
}
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Repeart
{
public partial class _Default : System.Web.UI.Page
{
string strCon = ConfigurationManager.ConnectionStrings["con"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bind();
}
}
private void bind()
{
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select top 10 * from city", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string a = GridView1.Rows[e.RowIndex].Cells[2].Text.ToString();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//string a = ((DataRowView)e.Item.DataItem)["id"].ToString();
//Response.Write(a);
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "del")
{
string a = e.CommandArgument.ToString();
Response.Write(a);
}
}
protected void btnRpt_Click(object sender, EventArgs e)
{
foreach (RepeaterItem Item in Repeater1.Items)
{
CheckBox chk = (CheckBox)Item.FindControl("ckbIndex");
if (chk.Checked)
{
//Page.ClientScript.RegisterStartupScript(GetType(), "111", "<script>alert('要批量删除的记录ID号:"+chk.Text+"');</script>");
Response.Write("要批量删除的记录ID号:"+chk.Text+"<br/>");
}
}
}
}
}