asp.net gridview realizes the operation code of selecting unselecting and deleting records

  • 2020-05-12 02:31:15
  • OfStack

gridview select all operation
 
'columns'=>array( 
array( 
'class'=>'CCheckBoxColumn', 
//'header'=>' Future generations ', 
//'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(' Batch update ', 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(" The execution process exceeds 30 Seconds, please update in batches! "); }}', 
));?></div> 
<div id="load" style="display: none; text-align:right; width:100%; padding-top:20px"> Update in progress, please wait ......</div> 
<script> 
var ids=jQuery("input[name='link-grid_c0[]']:checked").map(function(){ return $(this).val(); }); 
//alert(ids.length); 
</script> 

asp.net gridview realizes full selection, unselection and deletion of records
. In the 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=" Future generations " OnClick="Button1_Click" />  
<asp:Button ID="Button3" runat="server" Text=" Return to choose " OnClick="Button3_Click" /> 
<asp:Button ID="Button2" runat="server" Text=" Delete the selected item " OnClick="Button2_Click" /> 

.cs
 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
{ 
SetDataBinder(); 
} 
Button2.Attributes.Add("onclick","return confirm(' Are you sure you want to delete the selected record ?')"); 
} 
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> 
///  All records  
/// </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> 
///  Perform the delete operation , Deletes the selected item  
/// </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) + ","; 
} 
} 
// Remove the last comma , And I'm going to add my right hand  
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 
{ 
// Execute delete statement  
SqlCommand cmd = new SqlCommand(sql, conn); 
int delcount = Convert.ToInt32(cmd.ExecuteNonQuery()); 
Response.Write("<script>alert(' delete " + delcount + " The data ');</script>"); 
SetDataBinder(); 
} 
catch (Exception ex) 
{ 
Response.Write(ex.Message); 
} 
finally 
{ 
conn.Close(); 
} 
} 
/// <summary> 
///  The selected operation  
/// </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 ; 
} 
} 
} 

Related articles: