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 }

 

 

posted on 2012-03-07 17:41  梦幻桀倩  阅读(302)  评论(0编辑  收藏  举报

导航