DataGrid中加入CheckBox,并实现单选

原理就是用JS遍列所有CheckBox把所有CheckBox设为未被选中,然后在把单击的CheckBox设为选中

以下是.ASPX文件

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGridCheck.WebForm1"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif 
< HEAD >
None.gif  
< title > WebForm1 </ title >
None.gif  
< meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
None.gif  
< meta  content ="C#"  name ="CODE_LANGUAGE" >
None.gif  
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
None.gif  
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
ExpandedBlockStart.gifContractedBlock.gif  
< SCRIPT  language ="JavaScript" > dot.gif
InBlock.gif     
function SetCheckBoxState()
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif     
var dom=document.all;
InBlock.gif     
var el=event.srcElement;
InBlock.gif        
if(el.tagName=="INPUT"&&el.type.toLowerCase()=="checkbox")
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif  
for(i=0;i<dom.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif      
if(dom[i].tagName=="INPUT"&&dom[i].type.toLowerCase()=="checkbox")
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif   dom[i].checked
=false;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif     }

InBlock.gif     el.checked
=!el.checked;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif  
</ SCRIPT >
None.gif 
</ HEAD >
None.gif 
< body  MS_POSITIONING ="GridLayout" >
None.gif  
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif   
< FONT  face ="宋体" >
None.gif    
< asp:datagrid  id ="dg"  style ="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 40px"  runat ="server"
None.gif     Width
="440px"  AutoGenerateColumns ="False" >
None.gif     
< Columns >
None.gif      
< asp:TemplateColumn >
None.gif       
< ItemTemplate >
None.gif        
< asp:CheckBox  id ="chkExport"  runat ="server" ></ asp:CheckBox >
None.gif       
</ ItemTemplate >
None.gif      
</ asp:TemplateColumn >
None.gif      
< asp:BoundColumn  DataField ="IntegerValue" ></ asp:BoundColumn >
None.gif      
< asp:BoundColumn  DataField ="StringValue" ></ asp:BoundColumn >
None.gif      
< asp:BoundColumn  DataField ="CurrencyValue" ></ asp:BoundColumn >
None.gif     
</ Columns >
None.gif    
</ asp:datagrid >< asp:button  id ="Button1"  style ="Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 8px"  runat ="server"
None.gif     Text
="显示内容" ></ asp:button ></ FONT ></ form >
None.gif 
</ body >
None.gif
</ HTML >
None.gif


以下是CS文件

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
namespace  DataGridCheck
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif  
protected System.Web.UI.WebControls.DataGrid dg;
InBlock.gif 
InBlock.gif  
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
// 在此处放置用户代码以初始化页面
InBlock.gif
   if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    dg.DataSource
= CreateDataSource() ;
InBlock.gif    dg.DataBind();   
ExpandedSubBlockEnd.gif   }

InBlock.gif   
InBlock.gif   
ExpandedSubBlockEnd.gif  }

InBlock.gif  ICollection CreateDataSource() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   DataTable dt 
= new DataTable();
InBlock.gif   DataRow dr;
InBlock.gif 
InBlock.gif   dt.Columns.Add(
new DataColumn("IntegerValue"typeof(Int32)));
InBlock.gif   dt.Columns.Add(
new DataColumn("StringValue"typeof(string)));
InBlock.gif   dt.Columns.Add(
new DataColumn("CurrencyValue"typeof(double)));
InBlock.gif 
InBlock.gif   
for (int i = 0; i < 9; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    dr 
= dt.NewRow();
InBlock.gif 
InBlock.gif    dr[
0= i;
InBlock.gif    dr[
1= "Item " + i.ToString();
InBlock.gif    dr[
2= 1.23 * (i + 1);
InBlock.gif 
InBlock.gif    dt.Rows.Add(dr);
ExpandedSubBlockEnd.gif   }

InBlock.gif 
InBlock.gif   DataView dv 
= new DataView(dt);
InBlock.gif   
return dv;
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif  
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif  
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//
InBlock.gif   
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif   
//
InBlock.gif
   InitializeComponent();
InBlock.gif   
base.OnInit(e);
ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif  
/// 此方法的内容。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{    
InBlock.gif   
this.dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemDataBound);
InBlock.gif   
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif   
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif
InBlock.gif  
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    System.Web.UI.WebControls.CheckBox chkExport;
InBlock.gif   
foreach (DataGridItem dgItem in dg.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    chkExport
=(CheckBox)dgItem.FindControl("chkExport");
InBlock.gif    
if(chkExport.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Response.Write(
"<script>alert('"+dgItem.Cells[2].Text+""+dgItem.Cells[3].Text+"')</script>");
InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }
  
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif     
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if(e.Item.ItemIndex<0)  return;
InBlock.gif   
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     System.Web.UI.WebControls.CheckBox chkExport; 
InBlock.gif    chkExport
=(CheckBox)e.Item.FindControl("chkExport");
InBlock.gif    chkExport.Attributes.Add(
"OnClick","SetCheckBoxState()");
InBlock.gif    e.Item.Attributes.Add(
"onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='48d1cc'");
InBlock.gif    e.Item.Attributes.Add(
"onmouseout","this.style.backgroundColor=currentcolor");
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif

转载于:https://www.cnblogs.com/leeon/archive/2004/12/10/75061.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值