程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net gridview實現全選,反選與刪除記錄的操作代碼

asp.net gridview實現全選,反選與刪除記錄的操作代碼

編輯:ASP.NET基礎
gridview全選操作
復制代碼 代碼如下:
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
//'header'=>'全選',
//'value'=>'$data->id',
//'checked'=>'true',
'htmlOptions'=>array(
'width'=>'30',
'style'=>'text-align:center',
),
),

復制代碼 代碼如下:
<div id="btn" style="width:100%; text-align:right; padding-top:20px"><?php echo CHtml::ajaxLink('批量更新', array('link/dels'), array(
'type'=>'POST',
'timeout'=>'30000',
'data'=>'js:{ids:jQuery("input[name=\'link-grid_c0\[\]\']:checked").map(function(){ return $(this).val(); }).get()}',
'beforeSend'=>'function(){ $("#btn").hide(); $("#load").show(); }',
'success'=>'function(html){ alert(html); }',
'complete'=>'function(){ $("#btn").show(); $("#load").hide(); }',
'error'=>'function(a,b,c){ if(b=="timeout") { alert("本次執行過程超過30秒,請分批更新!"); }}',
));?></div>
<div id="load" style="display: none; text-align:right; width:100%; padding-top:20px">正在更新,請稍後......</div>
<script>
var ids=jQuery("input[name='link-grid_c0[]']:checked").map(function(){ return $(this).val(); });
//alert(ids.length);
</script>

asp.net gridview實現全選,反選與刪除記錄
.aspx中
復制代碼 代碼如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames ="MailID">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack ="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FormAddress" HeaderText="FormAddress" SortExpression="FormAddress" />
<asp:BoundField DataField="ToAddress" HeaderText="ToAddress" SortExpression="ToAddress" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Contents" HeaderText="Contents" SortExpression="Contents" />
<asp:BoundField DataField="Times" HeaderText="Times" SortExpression="Times" />
<asp:CheckBoxField DataField="ReadFlag" HeaderText="ReadFlag" SortExpression="ReadFlag" />
<asp:CheckBoxField DataField="UpFlag" HeaderText="UpFlag" SortExpression="UpFlag" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="全選" OnClick="Button1_Click" /> 
<asp:Button ID="Button3" runat="server" Text="返選" OnClick="Button3_Click" />
<asp:Button ID="Button2" runat="server" Text="刪除選中的項" OnClick="Button2_Click" />

.cs
復制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetDataBinder();
}
Button2.Attributes.Add("onclick","return confirm('你確定要刪除所選擇的記錄麼?')");
}
protected void SetDataBinder()
{
string sql = "Select * from SendMail";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["StudyConnectionString"].ToString());
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql ,conn );
DataSet ds = new DataSet();
da.Fill(ds,"table");
GridView1 .DataSource =ds.Tables ["table"];
GridView1.DataBind();
conn.Close();
}
/// <summary>
/// 全選記錄
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
CheckBox cb;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
cb.Checked = true;
}
}
/// <summary>
/// 執行刪除操作,刪除所選擇的項
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
string sql="(";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cb.Checked == true)
{
sql = sql + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
}
}
//去掉最後的逗號,並且加上右手號
sql = sql.Substring(0,sql.Length -1)+")";
sql = "delete SendMail where MailID in"+sql;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["StudyConnectionString"].ToString());
conn.Open();
try
{
//執行刪除語句
SqlCommand cmd = new SqlCommand(sql, conn);
int delcount = Convert.ToInt32(cmd.ExecuteNonQuery());
Response.Write("<script>alert('共刪除" + delcount + "條數據');</script>");
SetDataBinder();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
/// <summary>
/// 反選操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{
CheckBox cb;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (cb.Checked)
{
cb.Checked = false ;
}
else
{
cb.Checked = true ;
}
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved