第一次发表,不会写.直接把原文件给大家看算了!!
原理就是用js遍列所有checkbox把所有checkbox设为未被选中,然后在把单击的checkbox设为选中
以下是.aspx文件
<%@ page language="c#" codebehind="webform1.aspx.cs" autoeventwireup="false" inherits="datagridcheck.webform1" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>webform1</title>
<meta content="microsoft visual studio .net 7.1" name="generator">
<meta content="c#" name="code_language">
<meta content="javascript" name="vs_defaultclientscript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
<script language="javascript">
function setcheckboxstate()
{
var dom=document.all;
var el=event.srcelement;
if(el.tagname=="input"&&el.type.tolowercase()=="checkbox")
{
for(i=0;i<dom.length;i++)
{
if(dom[i].tagname=="input"&&dom[i].type.tolowercase()=="checkbox")
{
dom[i].checked=false;
}
}
}
el.checked=!el.checked;
}
</script>
</head>
<body ms_positioning="gridlayout">
<form id="form1" method="post" runat="server">
<font face="宋体">
<asp:datagrid id="dg" style="z-index: 101; left: 168px; position: absolute; top: 40px" runat="server"
width="440px" autogeneratecolumns="false">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:checkbox id="chkexport" runat="server"></asp:checkbox>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn datafield="integervalue"></asp:boundcolumn>
<asp:boundcolumn datafield="stringvalue"></asp:boundcolumn>
<asp:boundcolumn datafield="currencyvalue"></asp:boundcolumn>
</columns>
</asp:datagrid><asp:button id="button1" style="z-index: 102; left: 168px; position: absolute; top: 8px" runat="server"
text="显示内容"></asp:button></font></form>
</body>
</html>
以下是cs文件
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace datagridcheck
{
/// <summary>
/// webform1 的摘要说明。
/// </summary>
public class webform1 : system.web.ui.page
{
protected system.web.ui.webcontrols.button button1;
protected system.web.ui.webcontrols.datagrid dg;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
if (!page.ispostback)
{
dg.datasource= createdatasource() ;
dg.databind();
}
}
icollection createdatasource()
{
datatable dt = new datatable();
datarow dr;
dt.columns.add(new datacolumn("integervalue", typeof(int32)));
dt.columns.add(new datacolumn("stringvalue", typeof(string)));
dt.columns.add(new datacolumn("currencyvalue", typeof(double)));
for (int i = 0; i < 9; i++)
{
dr = dt.newrow();
dr[0] = i;
dr[1] = "item " + i.tostring();
dr[2] = 1.23 * (i + 1);
dt.rows.add(dr);
}
dataview dv = new dataview(dt);
return dv;
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.dg.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(this.dg_itemdatabound);
this.button1.click += new system.eventhandler(this.button1_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void button1_click(object sender, system.eventargs e)
{
system.web.ui.webcontrols.checkbox chkexport;
foreach (datagriditem dgitem in dg.items)
{
chkexport=(checkbox)dgitem.findcontrol("chkexport");
if(chkexport.checked)
{
response.write("<script>alert("+dgitem.cells[2].text+"和"+dgitem.cells[3].text+")</script>");
}
}
}
private void dg_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
{
if(e.item.itemindex<0) return;
if(e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
{
system.web.ui.webcontrols.checkbox chkexport;
chkexport=(checkbox)e.item.findcontrol("chkexport");
chkexport.attributes.add("onclick","setcheckboxstate()");
e.item.attributes.add("onmouseover","currentcolor=this.style.backgroundcolor;this.style.backgroundcolor=48d1cc");
e.item.attributes.add("onmouseout","this.style.backgroundcolor=currentcolor");
}
}
}
}