选择DataGrid中的CheckBox控件后该行背景变色

在网络开发中,经常遇到需要使用ASP.NET与javascript联合进行控制的情况。就像163邮箱一样,选中checkbox后即变色,在数据很多的情况下,可以让用户很精确的选中哪一行,不导致删除数据的错误。将使用DataGrid进行数据绑定,使用javascript控制当选中其中的checkbox时,该行颜色改变。

首先,在页面中创建一个DataGrid控件,并设置其模板。

None.gif < asp:DataGrid  id ="DataGrid1"  runat ="server"  AutoGenerateColumns ="False" >
None.gif
< Columns >
None.gif
< asp:TemplateColumn >
None.gif
< ItemTemplate >
None.gif
< asp:CheckBox  id ="checkbox1"  Runat  ="server" ></ asp:CheckBox >
None.gif
< asp:Label   runat ="server"  Text ='<%#  DataBinder.Eval(Container, "DataItem") % > '> </ asp:Label >
None.gif
</ ItemTemplate >
None.gif
</ asp:TemplateColumn >
None.gif
</ Columns >
None.gif
</ asp:DataGrid >
None.gif
None.gif

第二,在页面中的<head></head>中编写javascript脚本函数,进行CheckBox的判断和颜色改变的控制。

None.gif < script >   
ExpandedBlockStart.gifContractedBlock.gif   
function  checkme(obj,tr) dot.gif {
InBlock.gif   
if(obj.checked)
InBlock.gif      tr.style.backgroundColor
='blue';
InBlock.gif   
else
InBlock.gif      tr.style.backgroundColor
='';
ExpandedBlockEnd.gif    }

None.gif    
< / script>  
None.gif

第三,在Page_Load事件中为DataGrid绑定数据,并关联CheckBox的javascript脚本。

None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
// Put user code to initialize the page here
InBlock.gif
 if(!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  databind();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
private   void  databind()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif ArrayList arr
=new ArrayList();
InBlock.gif arr.Add(
"新闻综合");
InBlock.gif arr.Add(
"综艺");
InBlock.gif arr.Add(
"电影");
InBlock.gif arr.Add(
"教育");
InBlock.gif arr.Add(
"戏剧");
InBlock.gif arr.Add(
"军事");
InBlock.gif arr.Add(
"体育");
InBlock.gif DataGrid1.DataSource
=arr;
InBlock.gif DataGrid1.DataBind();  
InBlock.gif 
int i;
ExpandedSubBlockStart.gifContractedSubBlock.gif 
for(i=0;i<DataGrid1.Items.Count;i++)dot.gif{
InBlock.gif  CheckBox cb;
InBlock.gif  cb
=(CheckBox)DataGrid1.Items[i].FindControl("checkbox1"); 
InBlock.gif  DataGrid1.Items[i].Attributes.Add(
"id","tr" + i.ToString()); 
InBlock.gif  cb.Attributes.Add(
"onclick","checkme(this,tr" + i.ToString() + ");"); 
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif


 

转载于:https://www.cnblogs.com/conquer/archive/2008/04/01/1132772.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值