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

來源:互聯網
上載者:User

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 ;
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.