Jquery实现三层遍历删除功能代码
x页:
代码如下:
<script src="jquery1.7.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#checkbox1').click(function () {
if ($('#checkbox1').is(':checked')) {
$('td input').attr('checked', true);
}
else { $('td input').attr('checked', false); }
})
$('#checkbox2').click(function () {
$('td input').each(function () {
$(this).attr('checked', !$(this).attr('checked'))
})
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<p>
<input id="checkbox1" type="checkbox" />全选<input id="checkbox2" type="checkbox" />反选<asp:button
id="button1" runat="server" text="删除" onclick="button1_click" />
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false">
<columns>
<asp:templatefield headertext="选择">
<itemtemplate>
<asp:checkbox id="checkbox3" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="id" headertext="编号" />
<asp:boundfield datafield="newstitle" headertext="标题" />
<asp:boundfield datafield="newscontent" headertext="新闻内容">
<itemstyle width="200px" />
</asp:boundfield>
<asp:boundfield datafield="newscreator" headertext="创建者" />
<asp:boundfield datafield="createtime" headertext="创建时间" />
</columns>
</asp:gridview>
</p>
</form>
后台cs文件:
代码 代码如下:
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
load();
}
}
private void load()//就实现绑定
{
jquery三层实现删除功能.model.t_news mm = new model.t_news();
jquery三层实现删除功能.bll.t_news bb = new bll.t_news();
string sqlwhere = "id<25";
ds = bb.getlist(sqlwhere);//获取dataset绑定
this.gridview1.datasource = ds;
gridview1.databind();
}
protected void button1_click(object sender, eventargs e)
{
//遍历每一行
foreach (gridviewrow item in gridview1.rows)//一定要记住这里是gridviewrow不是datarow
{
checkbox cb = (checkbox)item.findcontrol("checkbox3");//找到这个选中项
if (cb.checked)//选中
{
jquery三层实现删除功能.bll.t_news bb = new bll.t_news();
if (bb.deletelist(item.cells[1].text))//调用bb.deletelist(id)删除
{
response.write("删除成功");
}
load();