asp.net gridview实现全选,反选与删除记录的操作代码

时间:2021-05-28

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" />&nbsp;
<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 ;
}
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章