如何在gridview中获取复选框的选中值

时间:2014-05-12 12:35:47

标签: c# asp.net

当我点击按钮时,我无法进入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();

                  }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您的数据是否仅首次绑定,即检查!IsPostBack condidition

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();

             }
        }

答案 1 :(得分:1)

TemplateField的工作方式与普通的CheckBoxField不同,因为您需要查看控件所在的单元格以查找CheckBox。因此,不要将您的复选框声明为:

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

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

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

答案 2 :(得分:0)

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

    <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 =&#34; RadioCheck(this);它会起作用