码迷,mamicode.com
首页 > 其他好文 > 详细

gridview中添加单选和多选

时间:2015-07-16 18:51:39      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

1.前台js     

<script type="text/javascript">
        function SetAll(obj) {
            var chk = document.getElementById("<%=this.GridView1.ClientID%>").getElementsByTagName("input");
            for (var i = 0; i < chk.length; i++) {
                if (chk[i].id = "chkOne") {
                    if (chk[i].disabled == false) {
                        chk[i].checked = obj.checked;
                    }
                }
            }
        }
    </script>       

2.gridview 单选和多选框的加法

<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick="SetAll(this)" Text="全选" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkOne" runat="server" />
</ItemTemplate>
</asp:TemplateField>

3.后台取出选中的行的信息

StringBuilder deptIDs = new StringBuilder();
        foreach (GridViewRow gvr in GridView2.Rows)
        {
            CheckBox chk = gvr.FindControl("chkOne") as CheckBox;
            if (chk.Checked)
            { deptIDs.Append(",").Append(GridView2.DataKeys[gvr.RowIndex]["RequestID"].ToString().Trim()); }
        }
        string[] sArray = deptIDs.ToString().Split(‘,‘);

注意:给gridview绑定DataKeyNames

 

gridview中添加单选和多选

标签:

原文地址:http://www.cnblogs.com/mjlblogs/p/4652019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!