GridView控件实现单选功能(CheckBox)

1 html代码
 1 <Columns>
2
3 <asp:TemplateField HeaderText="选择">
4 <ItemTemplate>
5 <asp:HiddenField runat="server" ID="bdc" Value='<%#Eval("户型编号") %>' />
6 <asp:CheckBox runat="server" ID="cbID" Checked="false" />
7 </ItemTemplate>
8 </asp:TemplateField>
9
10 <asp:BoundField DataField="户型编号" HeaderText="户型编号" />
11 <asp:BoundField DataField="楼盘名称" HeaderText="楼盘名称" />
12 <asp:BoundField DataField="单元" HeaderText="单元" />
13 <asp:BoundField DataField="户型" HeaderText="户型" />
14 <asp:BoundField DataField="面积" HeaderText="面积" />
15 <asp:BoundField DataField="楼层" HeaderText="楼层" />
16 <asp:BoundField DataField="价格" HeaderText="价格" />
17 <asp:BoundField DataField="使用年限" HeaderText="使用年限" />
18 <asp:BoundField DataField="特征" HeaderText="特征" />
19 <asp:BoundField DataField="小区" HeaderText="小区" Visible="False" />
20 <asp:CommandField ShowDeleteButton="True" />
21 </Columns>

 

2javascript脚本语言
 1 <script type="text/javascript">
2 function Change(SCHeckBox)
3 {
4 var objs = document.getElementsByTagName("input");
5 for(var i=0;i<objs.length;i++)
6 {
7 if(objs[i].type.toLowerCase()=="checkbox")
8 objs[i].checked=false;
9 }
10 var SelectCheckBoxID = SCHeckBox.id;
11 document.getElementById(SelectCheckBoxID).checked = true;
12 }
13 </script>

 

3 后台代码
 1 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 System.Web.UI.WebControls.CheckBox cbx = e.Row.FindControl("cbID") as System.Web.UI.WebControls.CheckBox;
4 try
5 {
6 //绑定选中CheckBox 客户端ID
7 cbx.Attributes.Add("onclick", "Change(" + cbx.ClientID + ")");
8 }
9 catch
10 {
11 }
12 if (e.Row.RowType == DataControlRowType.DataRow)
13 {
14 e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor ='#6699ff'");
15 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor =currentcolor");
16 }
17 }

 

 

转载于:https://www.cnblogs.com/MyMei/archive/2012/03/07/2383935.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值