繁体   English   中英

如何在GridView中获取复选框的检查值

[英]How to get checked value of check box in gridview

当我单击按钮时,我无法进入IF条件,因为即使选中了复选框,我也会得到复选框的错误值。 因此,请帮助我,如何在下面的代码中获取真正的价值?

我要求所有人在此错误中提供帮助。

这是我的aspx文件代码Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" DataKeyNames="USER_CODE">
        <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                    <asp:CheckBox ID="chk_row" runat="server" />

                    </ItemTemplate>
                </asp:TemplateField>

                <asp:BoundField DataField="USER_FULL_NAME" HeaderText ="User Full Name" />
                <asp:BoundField DataField="USER_DEPT_NAME" HeaderText ="Department" />
        </Columns>
    </asp:GridView>
    <br />
    <br />
    <asp:Button ID="btn_select" runat="server" Text="Selected DemandNote" 
            onclick="btn_select_Click" />
            <br />
            <br />
            <asp:Label ID="lbl_msg" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      DBConnect db = new DBConnect();
        SqlDataAdapter da = new SqlDataAdapter("SELECT USER_CODE,USER_FULL_NAME,USER_DEPT_NAME FROM USER_MASTER",db.connect());

        DataSet ds = new DataSet();

        da.Fill(ds);
        grd.DataSource = ds;
        grd.DataBind();
    }
    protected void btn_select_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvrw in grd.Rows)
        {
            if (gvrw.RowType == DataControlRowType.DataRow)
            {

                CheckBox chk = (CheckBox)gvrw.FindControl("chk_row");
                if (chk.Checked)
                {
                    lbl_msg.Text = grd.DataKeys[gvrw.RowIndex].Value.ToString();

                  }
            }
        }
    }
}

您的数据仅是第一次绑定吗,即检查!IsPostBack条件

protected void Page_Load(object sender, EventArgs e)
        {
          DBConnect db = new DBConnect();
            SqlDataAdapter da = new SqlDataAdapter("SELECT USER_CODE,USER_FULL_NAME,USER_DEPT_NAME FROM USER_MASTER",db.connect());

            DataSet ds = new DataSet();

            da.Fill(ds);
               if(!isPostBack)
              {

            grd.DataSource = ds;
            grd.DataBind();

             }
        }

TemplateField与普通CheckBoxField的工作原理不同,因为您需要查看控件要找到CheckBox的单元格。 因此,不必像这样声明您的复选框:

CheckBox chk = (CheckBox)gvrw.FindControl("chk_row");

您需要通过以下方式找到CheckBox:

CheckBox chk = (CheckBox)gvrw.Cells[0].FindControl("chk_row");

这是一个带有复选框的网格视图,

    <asp:GridView runat="server" ID="AgencyGrid" OnRowCommand="AgencyGrid_RowCommand" AutoGenerateColumns="False" BackColor="White"
                            BorderColor="#333534" BorderStyle="None" BorderWidth="1px" CellPadding="4" PageSize="30"
                            ForeColor="#333534" GridLines="Horizontal" HorizontalAlign="Center" Width="100%" DataSourceID="AgenciesList">
                            <Columns>
                                <asp:BoundField DataField="ID" HeaderText="ID" Visible="false" SortExpression="ID" />                                    
                                        <asp:CheckBox runat="server" AutoPostBack="true" ID="ShowDetailsForThisAgency" OnCheckedChanged="ShowDetailsForThisAgency_CheckedChanged" Onclick="RadioCheck(this);" value='<%# Eval("ID") %>' />
                                    </ItemTemplate>
</asp:GridView>

现在为了获取选择复选框,您将需要JS:

<script type="text/javascript">
    function RadioCheck(rb) {
        var gv = document.getElementById("<%=AgencyGrid.ClientID%>");
        var rbs = gv.getElementsByTagName("input");
        var row = rb.parentNode.parentNode;
        for (var i = 0; i < rbs.length; i++) {
            if (rbs[i].type == "checkbox") {
                if (rbs[i].checked && rbs[i] != rb) {
                    rbs[i].checked = false;
                    break;
                }
            }
        }
    }
</script>

在此代码中,我正在使用复选框检查事件,但是您可以在按钮单击事件中使用它,此最后一个代码将使您处于选中状态:

foreach (GridViewRow row in this.AgencyGrid.Rows)
                {
                    CheckBox checkedAgency = (CheckBox)row.FindControl("ShowDetailsForThisAgency");
                    if (checkedAgency.Checked)
                    {
                        do your logic 
                    }
}

注意:我正在使用JS允许用户仅在不需要的情况下选中复选框,则可以删除Onclick =“ RadioCheck(this);它将起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM