DataGrid中加入CheckBox,并实现单选

1@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGridCheck.WebForm1" 
 1<html>
 2<head>
 3<title>WebForm1</title>
 4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
 8<script language="JavaScript">   
 9function SetCheckBoxState()   
10{   
11var dom=document.all;   
12var el=event.srcElement;   
13if(el.tagName=="INPUT"&&el.type.toLowerCase()=="checkbox")   
14{   
15for(i=0;i<dom.length;i++)   
16{   
17if(dom[i].tagName=="INPUT"&&dom[i].type.toLowerCase()=="checkbox")   
18{   
19dom[i].checked=false;   
20}   
21}   
22}   
23el.checked=!el.checked;   
24}   
25</script>
26</head>
27<body ms_positioning="GridLayout">
28<form id="Form1" method="post" runat="server">
29<font face="宋体">
30<asp:datagrid autogeneratecolumns="False" id="dg" runat="server" style="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 40px" width="440px">
31<columns>
32<asp:templatecolumn>
33<itemtemplate>
34<asp:checkbox id="chkExport" runat="server"></asp:checkbox>
35</itemtemplate>
36</asp:templatecolumn>
37<asp:boundcolumn datafield="IntegerValue"></asp:boundcolumn>
38<asp:boundcolumn datafield="StringValue"></asp:boundcolumn>
39<asp:boundcolumn datafield="CurrencyValue"></asp:boundcolumn>
40</columns>
41</asp:datagrid><asp:button id="Button1" runat="server" style="Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 8px" text="显示内容"></asp:button></font></form>
42</body>
43</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
{
///

1<summary>   
2/// WebForm1 的摘要说明。   
3/// </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);
}

///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </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("

1<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");
}
}

}
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus