asp.Netgridview全選択、レコードの逆選択と削除を実現する操作コード
6127 ワード
gridview全選択操作
asp.Netgridviewは全選択、逆選択と削除記録を実現
.aspxで
.cs
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
//'header'=>' ',
//'value'=>'$data->id',
//'checked'=>'true',
'htmlOptions'=>array(
'width'=>'30',
'style'=>'text-align:center',
),
),
'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 , !"); }}',
));?>
<br>var ids=jQuery("input[name='link-grid_c0[]']:checked").map(function(){ return $(this).val(); });
<br>//alert(ids.length);
<br>
asp.Netgridviewは全選択、逆選択と削除記録を実現
.aspxで
.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();
}
///
///
///
///
///
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;
}
}
///
/// ,
///
///
///
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("alert(' " + delcount + " '); ");
SetDataBinder();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
///
///
///
///
///
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 ;
}
}
}