Repeater 实现批量,删除.全选.分页
分页用别人的.
js代码
<script language="javascript" type="text/javascript">
function selectall(chkcontrol)
{
var chkall=chkcontrol;
State=chkall.checked;
elem=chkall.form.elements;
for (i=0;i<elem.length;i++)
{
if(elem[i].type=="checkbox" && elem[i]!=chkall.id)
{
if(elem[i].checked!=State)
{
elem[i].click();
}
}
}
}
</script>
<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>
aspx页面
<div class="heisemoban_yinyue_3">
<div class="heisemoban_yinyue_4">
<div class="heisemoban_yinyue_5">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="6%" height="25">
<asp:CheckBox ID="Checkall" runat="server" onclick="javascript:selectall(this);" />
</td>
<td width="8%" height="25" style="font-size: 14px; color: #0058B0">
全选</td>
<td width="14%" height="25" style="font-size: 14px; color: #0058B0">
<asp:LinkButton ID="lbbofang" runat="server">连续播放</asp:LinkButton>
</td>
<td width="52%" height="25" style="font-size: 14px; color: #0058B0">
<asp:LinkButton ID="lbdelete" runat="server" OnClick="lbdelete_Click" CommandName="Delete" CommandArgument='<%# Eval("musicId") %>'
OnClientClick="return confirm( '你确定要删除这条记录? '); ">删除</asp:LinkButton>
</td>
<td width="20%" height="25" style="font-size: 14px; color: #0058B0">
<a href="T_music_Add.aspx">
添加网络音乐</a>
</td>
</tr>
</table>
</div>
</div>
<div class="heisemoban_yinyue_6">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="5%" height="24">
</td>
<td width="54%" height="24" style="font-weight: bold">
音乐名称
</td>
<td width="15%" height="24" style="font-weight: bold">
背景音乐</td>
<td width="9%" height="24" style="font-weight: bold">
修改</td>
<td width="8%" height="24" style="font-weight: bold">
移动</td>
<td width="9%" height="24" style="font-weight: bold">
删除</td>
</tr>
<tr>
<td height="1" colspan="6">
<img src="images/heisexuanxian_03.jpg" /></td>
</tr>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<td align="left" style="height: 30px">
<asp:CheckBox runat="server" ID="ckbIndex" Text='<%#Eval("musicId") %>' />
</td>
<td align="left" style="height: 30px">
<%# Eval("Name") %>
</td>
<td align="left" style="height: 30px">
<%# showislogin(Eval("beijing"))%>
</td>
<td align="left" style="height: 30px">
<a href="T_music_App.aspx?id=<%# Eval("musicId") %>">修改</a>
</td>
<td align="left" style="height: 30px">
<a href="javascript:void(0)" onclick="showFloat()">
移动 </a></td>
<td align="left" style="height: 30px">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" CommandArgument='<%# Eval("musicId") %>'
OnClientClick="return confirm( '你确定要删除这条记录? '); ">删除</asp:LinkButton>
</td>
</tr>
<tr>
<td height="1" colspan="6">
<img src="images/heisexuanxian_03.jpg" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr>
<td height="50">
<input title="选中/取消选中" onclick="CheckAll(this.form)" type="checkbox" name="chkall"
id="Checkbox1" /></td>
<td height="50" colspan="5">
<a href="#">全选</a> <a href="#">连续播放</a> <a href="#">
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click" OnClientClick="return confirm('确定要删除吗?')">删除</asp:LinkButton></a>
</td>
</tr>
<tr>
<td height="30" colspan="6" style="background-color: #202125">
<samp style="margin-left: 15px;">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%" OnPageChanged="AspNetPager1_PageChanged"
HorizontalAlign="center" CustomInfoHTML="共%PageCount%页,共%RecordCount%条,当前为第%CurrentPageIndex%页,每页%PageSize%条"
FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" NumericButtonTextFormatString=" {0} "
PrevPageText="上一页" ShowPageIndexBox="Always" SubmitButtonText="Go" PagingButtonSpacing="10px"
PageSize="10" TextAfterPageIndexBox="页" TextBeforePageIndexBox="转到" UrlPaging="True"
Height="30px" ShowCustomInfoSection="Right">
</webdiyer:AspNetPager>
</samp>
</td>
</tr>
</table>
</div>
</div>
cs页面
int intmusicId = 0;
protected void Page_Load(object sender, EventArgs e)
{
//intmusicId = int.Parse(Request.QueryString["id"].ToString());
if (!IsPostBack)
{
info_show();
}
}
protected void info_show()
{
//string strSql = "SELECT musicId,Name FROM [music] where sortId=2";
//DataTable dt = DAL.SqlServer2000.RunSelect(strSql);
//Repeater1.DataSource = dt;
//Repeater1.DataBind();
int pagesize = AspNetPager1.PageSize;//分页数.每页多少条数据
int pageindex = AspNetPager1.CurrentPageIndex;//第几页
string str1 = "SELECT * FROM music where sortId=2 ORDER BY musicId desc";//返回记录总数
string str2 = " SELECT TOP " + pagesize + " * FROM music WHERE sortId=2 and musicId NOT IN ( SELECT TOP " + pagesize * (pageindex - 1) + " musicId FROM music ORDER BY musicId ) ORDER BY musicId desc";
DataTable dt = DAL.SqlServer2000.RunSelect(str1);
int count = dt.Rows.Count;//记录总数
AspNetPager1.RecordCount = count;//记录总数
AspNetPager1.DataBind();
dt = DAL.SqlServer2000.RunSelect(str2);
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
//分页
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
info_show();
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "Delete")
{
//string strsql = "update music set where musicId=" + id;//放入回收站
string strsql = "delete FROM [music] where musicId=" + id;//这句是直接删除.删除语句
//Response.Write(strsql);
DAL.SqlServer2000.RunSql(strsql);
ClassMessage.ShowAndRedirect(this.Page, "删除成功!", "T_music_sort.aspx");
}
if (e.CommandName == "del")
{
string a = e.CommandArgument.ToString();
Response.Write(a);
}
}
protected void LinkButton3_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/>");
string strsql = "delete FROM music where musicId=" + chk.Text;//这句是直接删除.删除语句
//Response.Write(strsql);
//Response.Write(strsql);
DAL.SqlServer2000.RunSql(strsql);
ClassMessage.ShowAndRedirect(this.Page, "删除成功!", "T_music_sort.aspx");
}
}
}
protected void lbdelete_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/>");
string strsql = "delete FROM music where musicId=" + chk.Text;//这句是直接删除.删除语句
//Response.Write(strsql);
//Response.Write(strsql);
DAL.SqlServer2000.RunSql(strsql);
ClassMessage.ShowAndRedirect(this.Page, "删除成功!", "T_music_sort.aspx");
}
}
}