ASP.NET Gridview 中使用checkbox刪除的2種方法實(shí)例分享
更新時(shí)間:2013年06月09日 15:57:23 作者:
ASP.NET Gridview 中使用checkbox刪除的2種方法實(shí)例分享,需要的朋友可以參考一下
方法一:
后臺(tái)代碼:
protected void btn_delete_Click(object sender, EventArgs e)
{
for (int i = 0; i <this.GridView1.Rows.Count; i++)
{
int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value);
if ((this.GridView1.Rows[i].Cells[0].FindControl("CheckBox1") as CheckBox).Checked == true)
{
Delete(id);
ClientScript.RegisterStartupScript(GetType(),"提示","<script>alert('刪除成功!')</script>");
}
}
this.GridView1.DataBind();
}//刪除
private void Delete(int id)
{
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "delete from Notice_Msg where id=@id";
comm.Parameters.Add(new SqlParameter("@id", id));
comm.ExecuteNonQuery();
}
}
前臺(tái)代碼:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id">
另外還得添加一列,讓其綁定的字段為id,并且把這一列的visable屬性設(shè)為false
方法二:
后臺(tái):
protected void btn_delete_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox ckb = row.Cells[2].FindControl("CheckBox1") as CheckBox;
if (ckb.Checked)
{
using (SqlConnection sqlCnn = new SqlConnection(str))
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
{
sqlCmm.CommandText = "delete from Regime_Table where id='" + row.Cells[0].Text + "' ";
sqlCnn.Open();
int a= sqlCmm.ExecuteNonQuery();
if (a>0)
{
ClientScript.RegisterStartupScript(GetType(),"提示","<script>alert('刪除成功!')</script>");
}
else
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('刪除失?。?)</script>");
}
this.DataBind();
}
}
}
}
}
}
前臺(tái):
<style type="text/css">
.Hidden
{
display:none;
}
</style>
<asp:BoundField DataField="id" HeaderText="編號" >
<HeaderStyle CssClass="Hidden" />
<ItemStyle CssClass="Hidden" />
</asp:BoundField>
新增加一列,這一列綁定id字段,并且visable屬性不能為false,否則取不出值來。
checkbox全選功能:
<script type="text/jscript">
function change(sender) {
var table = document.getElementById("GridView1");
for (var i = 1; i < table.rows.length; i++) {
table.rows[i].cells[1].getElementsByTagName("input")[0].checked = sender.checked;
}
}
</script>
<HeaderTemplate>
<input id="Checkbox2" type="checkbox" onclick="change(this)"/>
全選
</HeaderTemplate>
后臺(tái)代碼:
復(fù)制代碼 代碼如下:
protected void btn_delete_Click(object sender, EventArgs e)
{
for (int i = 0; i <this.GridView1.Rows.Count; i++)
{
int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value);
if ((this.GridView1.Rows[i].Cells[0].FindControl("CheckBox1") as CheckBox).Checked == true)
{
Delete(id);
ClientScript.RegisterStartupScript(GetType(),"提示","<script>alert('刪除成功!')</script>");
}
}
this.GridView1.DataBind();
}//刪除
private void Delete(int id)
{
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "delete from Notice_Msg where id=@id";
comm.Parameters.Add(new SqlParameter("@id", id));
comm.ExecuteNonQuery();
}
}
前臺(tái)代碼:
復(fù)制代碼 代碼如下:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id">
另外還得添加一列,讓其綁定的字段為id,并且把這一列的visable屬性設(shè)為false
方法二:
后臺(tái):
復(fù)制代碼 代碼如下:
protected void btn_delete_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox ckb = row.Cells[2].FindControl("CheckBox1") as CheckBox;
if (ckb.Checked)
{
using (SqlConnection sqlCnn = new SqlConnection(str))
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
{
sqlCmm.CommandText = "delete from Regime_Table where id='" + row.Cells[0].Text + "' ";
sqlCnn.Open();
int a= sqlCmm.ExecuteNonQuery();
if (a>0)
{
ClientScript.RegisterStartupScript(GetType(),"提示","<script>alert('刪除成功!')</script>");
}
else
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('刪除失?。?)</script>");
}
this.DataBind();
}
}
}
}
}
}
前臺(tái):
復(fù)制代碼 代碼如下:
<style type="text/css">
.Hidden
{
display:none;
}
</style>
<asp:BoundField DataField="id" HeaderText="編號" >
<HeaderStyle CssClass="Hidden" />
<ItemStyle CssClass="Hidden" />
</asp:BoundField>
新增加一列,這一列綁定id字段,并且visable屬性不能為false,否則取不出值來。
checkbox全選功能:
復(fù)制代碼 代碼如下:
<script type="text/jscript">
function change(sender) {
var table = document.getElementById("GridView1");
for (var i = 1; i < table.rows.length; i++) {
table.rows[i].cells[1].getElementsByTagName("input")[0].checked = sender.checked;
}
}
</script>
<HeaderTemplate>
<input id="Checkbox2" type="checkbox" onclick="change(this)"/>
全選
</HeaderTemplate>
相關(guān)文章
mstest實(shí)現(xiàn)類似單元測試nunit中assert.throws功能
我們做單元測試NUnit中,有一個(gè)斷言Assert.Throws很好用,現(xiàn)在我們來擴(kuò)展一下也實(shí)現(xiàn)類似成功能,大家參考使用吧2014-01-01asp.net+sqlserver實(shí)現(xiàn)的簡單高效的權(quán)限設(shè)計(jì)示例
大部分系統(tǒng)都有權(quán)限系統(tǒng)。一般來說,它能管控人員對某個(gè)否頁面的訪問;對某些字段、控件可見或者不可見。對gridview中的數(shù)據(jù)是否可刪除、可添加、可新增等等。2010-04-04關(guān)于多對多關(guān)系表無法更新與插入的問題
這篇文章主要介紹了關(guān)于多對多關(guān)系表無法更新與插入的問題 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07深入Lumisoft.NET組件POP3郵件接收與刪除操作的使用詳解
本篇文章對Lumisoft.NET組件POP3郵件接收與刪除操作的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05ASP.NET網(wǎng)頁打印(只打印相關(guān)內(nèi)容/自寫功能)
朋友要求在前段時(shí)間完成的新聞的網(wǎng)站上加上一個(gè)功能,就是在每篇新聞瀏覽的頁面, 加一個(gè)打印銨鈕。讓用戶一點(diǎn)打印,能把整篇文章打印2013-01-01.net 中按.(點(diǎn))無法智能提示的bug解決方案
IDE按.無法智能提示,但是可以編譯并正常使用,在修改別人代碼bug時(shí)遇到的,接下來為你提供詳細(xì)解決方法,感興趣的你可以參考下哈2013-03-03