gridview全选操作
复制代码 代码如下:
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
//'header'=>'全选',
//'value'=>'$data->id',
//'checked'=>'true',
'htmlOptions'=>array(
'width'=>'30',
'style'=>'text-align:center',
),
),
复制代码 代码如下:
<div id="btn"><?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">正在更新,请稍后......</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="全选" />
<asp:Button ID="Button3" runat="server" Text="返选" />
<asp:Button ID="Button2" runat="server" Text="删除选中的项" />
.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 ;
}
}
}