zoukankan      html  css  js  c++  java
  • gridview中实现checkbox的单选处理

    gridView中

    <asp:TemplateField HeaderText="选择">
          <ItemTemplate>
                  <asp:CheckBox ID="cbId" checked="false" runat="server" />
          </ItemTemplate>
    </asp:TemplateField>

    gridview的RowDataBound事件

        /// <summary>
        /// 列表数据行绑定事件处理程序 
         /// </summary>
        /// <param name="sender">事件对象</param>
        /// <param name="e">事件参数</param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //为每个checkbox绑定setRadio事件
            System.Web.UI.WebControls.CheckBox cbx = e.Row.FindControl("cbId") as System.Web.UI.WebControls.CheckBox;
            try
            {
                //绑定选中CheckBox客户端ID             
                cbx.Attributes.Add("onclick", "Change(" + cbx.ClientID + ")");
                GridViewRow item = e.Row;
                if (item.RowType.Equals(DataControlRowType.DataRow))
                {
                    CustomerCardInfosInfo customerCardModel = (CustomerCardInfosInfo)item.DataItem;
                    //卡状态
                    item.Cells[4].Text = CommonOperation.GetStatus(customerCardModel.Status);
                }
            }
            catch
            {
            }
        }

    前台js实现checkbox单选效果

    <script type="text/javascript">
                function Change(SCHeckBox) {
                    var objs = document.getElementsByTagName("input");
                    for (var i = 0; i < objs.length; i++) {
                        if (objs[i].type.toLowerCase() == "checkbox")
                            objs[i].checked = false;
                    }
                    var SelectCheckBoxID = SCHeckBox.id;
                    document.getElementById(SelectCheckBoxID).checked = true;
                }
    </script>

    后台获取gridview列表值

            foreach (GridViewRow gvr in gvList.Rows)
            {
                if (((CheckBox)gvr.FindControl("cbId")).Checked)
                {
                    int index = gvr.RowIndex;
    
                    asn = gvList.Rows[index].Cells[1].Text;
                    driverName = gvList.Rows[index].Cells[2].Text;
                    vehicleNo = gvList.Rows[index].Cells[3].Text;
                }
            }
    为易维护、易扩展、易复用、灵活多样而努力~~
  • 相关阅读:
    【NIO】NIO之浅谈内存映射文件原理与DirectMemory
    【搜索引擎】全文索引数据结构和算法
    【多线程】并发与并行
    【缓存】缓存穿透、缓存雪崩、key重建方案
    布隆过滤器
    多层路由器通信
    【路由】设置二级路由器
    【硬件】集线器,交换机,路由器
    JZOJ100048 【NOIP2017提高A组模拟7.14】紧急撤离
    JZOJ100045 【NOIP2017提高A组模拟7.13】好数
  • 原文地址:https://www.cnblogs.com/SpringDays/p/3130539.html
Copyright © 2011-2022 走看看