欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-06 20:28:02
gridview全选操作 复制代码 代码如下: 'columns'=>array( array( 'class'=>'ccheckboxcolumn', //'...
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 ;
}
}
}