我在gridview的模板列放一个checkbox,现在要将选中checkbox的行从表中删除,如何去做?

 我来答
xiaowei6400397
2011-07-04
知道答主
回答量:35
采纳率:0%
帮助的人:0
展开全部
页面aspx
<center>
<div>
<asp:GridView ID="GridViewStudent" runat="server" AutoGenerateColumns ="false"
BorderColor="Tan" BorderWidth="1px"
CellPadding="2" ForeColor="Black" Width ="300px" Height ="300px">
<Columns >
<asp:TemplateField HeaderStyle-BorderWidth ="1px" ItemStyle-BorderWidth ="1px" >
<HeaderTemplate >
<asp:CheckBox ID="CheckBoxAll" runat="server" />
</HeaderTemplate>
<ItemTemplate >
<asp:CheckBox ID="CheckBoxSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="学号" HeaderStyle-BorderWidth ="1px" ItemStyle-BorderWidth ="1px">
<ItemTemplate >
<asp:Label runat ="server" Text ='<%# Eval("stuID") %>' ID="lalStuID" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="年龄" HeaderStyle-BorderWidth ="1px" ItemStyle-BorderWidth ="1px">
<ItemTemplate >
<asp:Label runat ="server" Text ='<%# Eval("stuAge")%>' ID="lalstuAge" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="名字" HeaderStyle-BorderWidth ="1px" ItemStyle-BorderWidth ="1px" >
<ItemTemplate >
<asp:Label runat ="server" Text ='<%# Eval("stuName")%>' ID="lalstuName" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">选中删除</asp:LinkButton>
                             
<asp:LinkButton ID="LinkButton2" runat="server" OnClientClick ="javascript:window.close();return false;">关闭本页</asp:LinkButton>
</div>
</center>
后台aspx .cs
public partial class _Default : System.Web.UI.Page
{
static string strCon = "Data Source=WWW-FB9070E036C;Initial Catalog=Student;Integrated Security=True";
static SqlConnection con;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Bind();
}
}
public void Bind()
{

string strSQL = "select*from dbo.Student1";
GridViewStudent.DataSource = GetTable(strSQL);
GridViewStudent.DataBind();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
foreach (GridViewRow item in GridViewStudent.Rows)
{
bool bl = ((CheckBox)GridViewStudent.Rows[Convert.ToInt32(item.RowIndex)].FindControl("CheckBoxSelect")).Checked;
int lblStuId = Convert.ToInt32(((Label)GridViewStudent.Rows[item.RowIndex].FindControl("lalStuID")).Text);
if (bl == true)
{
string strSQL = string.Format("delete from Student1 where stuID ={0}", lblStuId);
GetNonQuery(strSQL);
}
}
Bind();
}
public void GetCon()
{
con = new SqlConnection(strCon);
if (con.State==ConnectionState .Open)
{
con.Close();
}
con.Open();
}
public int GetNonQuery(string strSql)
{
GetCon();
SqlCommand com = new SqlCommand(strSql, con);
return com.ExecuteNonQuery();
}
public DataTable GetTable(string strSql)
{
GetCon();
SqlDataAdapter sdr = new SqlDataAdapter(strSql, con);
DataTable dt = new DataTable();
sdr.Fill(dt);
return dt;
}
}
匿名用户
2011-07-06
展开全部
<%--全选--%>

<script type="text/javascript">
//根据传入的checkbox的选中状态设置所有checkbox的选中状态
function selectAll(obj)
{
var allInput = document.getElementsByTagName("input");
//alert(allInput.length);
var loopTime = allInput.length;
for(i = 0;i < loopTime;i++)
{
//alert(allInput[i].type);
if(allInput[i].type == "checkbox")
{
allInput[i].checked = obj.checked;
}
}
}
// 删除
function judgeSelect()
{
var result = false;
var allInput = document.getElementsByTagName("input");
var loopTime = allInput.length;
for(i = 0;i < loopTime;i++)
{
if(allInput[i].checked)
{
result = true;
break;
}
}
if(!result)
{
alert("请先选则要删除的记录!");
return result;
}
result = confirm("你确认要删除选定的记录吗?");
return result;
}

</script>

<asp:GridView ID="gdvAdvertise" runat="server" AutoGenerateColumns="false" GridLines="None"
Width="603px" CssClass="gdvAdvertise">
<Columns>
<asp:TemplateField HeaderText="选择" HeaderStyle-CssClass="gdvAdvertise_Header1">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" />
</ItemTemplate>
<ItemTemplate>
<input id="cbItem" name="cbItem" type="checkbox" value='<%# Eval("advId") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Height="35px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="编号" HeaderStyle-Width="50px" ItemStyle-CssClass="gdvAdvertise_Item">
<ItemTemplate>
<%#Eval ("advId")%>
</ItemTemplate>
<HeaderStyle CssClass="gdvAdvertise_Header"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="广告名称" HeaderStyle-CssClass="gdvAdvertise_Header" ItemStyle-CssClass="gdvAdvertise_Item">
<ItemTemplate>
<a href='UpdateAdvertise.aspx?<%#Eval("advId") %>' target="_blank" title='<%#Eval("advName") %>'>
<asp:Label ID="Label1" runat="server" Text='<%#StringTruncat(Eval("advName").ToString(),8,"...") %>'></asp:Label>
</a>
</ItemTemplate>
<HeaderStyle CssClass="gdvAdvertise_Header"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="广告发布时间" HeaderStyle-CssClass="gdvAdvertise_Header">
<ItemTemplate>
<%#Eval("advTime", "")%>
</ItemTemplate>
<HeaderStyle CssClass="gdvAdvertise_Header"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="发布者" HeaderStyle-CssClass="gdvAdvertise_Header1">
<ItemTemplate>
<%#Eval("superName")%>
</ItemTemplate>
<HeaderStyle CssClass="gdvAdvertise_Header1"></HeaderStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:CheckBox ID="CheckBox2" runat="server" onclick="selectAll(this)" Text="全选" CssClass="advertise_checkbox" />
用javascript实现的

后台页面.cs
//删除广告按钮事件
protected void imagbtdelet_Click(object sender, ImageClickEventArgs e)
{

if (CheckBox2.Checked == true)
{
new AdvertiseManager().DeleteAllAdvertise();

Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除成功');</script>");
//重新绑定
gdvAdvertise.DataSource = new AdvertiseManager().SelectAllAdvertise();
gdvAdvertise.DataBind();
}
else if (Request.Form["cbItem"] != null)
{
int ids = Convert.ToInt32(Request.Form["cbItem"]);
new AdvertiseManager().DeletAdvertise(ids);

Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除成功');</script>");
//重新绑定
gdvAdvertise.DataSource = new AdvertiseManager().SelectAllAdvertise();
gdvAdvertise.DataBind();
}

}是利用3层架构的
另外,团IDC网上有许多产品团购,便宜有口碑
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式