• 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;
                }
            }
    为易维护、易扩展、易复用、灵活多样而努力~~
  • 相关阅读:
    DUILib的代码分析
    source$表坏块
    树莓派风扇自动控制随想
    给qq机器人加上bing搜索
    龙芯fedora28日常生存指南
    攻防世界 when_did_you_born
    部署PWN题Docker环境
    NPUCTF2020 EzRSA
    金融密码杯 The Art of War
    Machine Learning & Deep Learning Fundamentals
  • 原文地址:https://www.cnblogs.com/SpringDays/p/3130539.html
Copyright © 2020-2023  润新知