ASP.NET Gridview 中使用checkbox刪除的2種方法實例分享
更新時間:2013年06月09日 15:57:23 作者:
ASP.NET Gridview 中使用checkbox刪除的2種方法實例分享,需要的朋友可以參考一下
方法一:
后臺代碼:
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();
}
}
前臺代碼:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id">
另外還得添加一列,讓其綁定的字段為id,并且把這一列的visable屬性設為false
方法二:
后臺:
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();
}
}
}
}
}
}
前臺:
<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>
后臺代碼:
復制代碼 代碼如下:
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();
}
}
前臺代碼:
復制代碼 代碼如下:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id">
另外還得添加一列,讓其綁定的字段為id,并且把這一列的visable屬性設為false
方法二:
后臺:
復制代碼 代碼如下:
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();
}
}
}
}
}
}
前臺:
復制代碼 代碼如下:
<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>
相關文章
mstest實現(xiàn)類似單元測試nunit中assert.throws功能
我們做單元測試NUnit中,有一個斷言Assert.Throws很好用,現(xiàn)在我們來擴展一下也實現(xiàn)類似成功能,大家參考使用吧2014-01-01asp.net+sqlserver實現(xiàn)的簡單高效的權限設計示例
大部分系統(tǒng)都有權限系統(tǒng)。一般來說,它能管控人員對某個否頁面的訪問;對某些字段、控件可見或者不可見。對gridview中的數(shù)據(jù)是否可刪除、可添加、可新增等等。2010-04-04深入Lumisoft.NET組件POP3郵件接收與刪除操作的使用詳解
本篇文章對Lumisoft.NET組件POP3郵件接收與刪除操作的使用進行了詳細的介紹。需要的朋友參考下2013-05-05