【问题标题】:GridView with CheckBox: How To Get Selected Rows in ASP.Net带 CheckBox 的 GridView:如何在 ASP.Net 中获取选定的行
【发布时间】:2015-06-21 10:05:49
【问题描述】:

选中复选框时如何获取 gridview 行值。我在按钮的单击事件中使用此代码,但它不起作用。

HTML代码:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
        DataKeyNames="ReportId" OnRowDataBound="GridView2_OnRowDataBound" ForeColor="#333333"
        PageSize="5" Style="text-align: center">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBoxG1" runat="server" />
                </ItemTemplate>

C#代码:

    protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox CheckRow = (row.Cells[0].FindControl("CheckBoxG1") as CheckBox);
            if (CheckRow.Checked)
            {

            }
        }
    }
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我看不到您如何绑定数据以及您的按钮放置在哪里。所以这是工作样本。

    <asp:Button Text="text" runat="server" OnClick="Unnamed_Click" />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ReportId" Width="100%"
                ForeColor="#333333" PageSize="5" Style="text-align: center">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBoxG1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataSource = new RowModel[]
                {
                    new RowModel { ReportId = "1" },
                    new RowModel { ReportId = "2" },
                    new RowModel { ReportId = "3" }
                };
    
                GridView1.DataBind();
            }
        }
    
        protected void Unnamed_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox CheckRow = (row.Cells[0].FindControl("CheckBoxG1") as CheckBox);
                    if (CheckRow.Checked)
                    {
    
                    }
                }
            }
        }
    
    public class RowModel
    {
        public string ReportId { get; set; }
    }
    

    【讨论】:

      【解决方案2】:

      在我的示例代码中,我在绑定 gridview 时考虑了手动数据,因为您没有指定如何通过数据库绑定 gridview,但它应该适用于两种方法。 我的 HTML 代码

      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
          <title>Get Checkbox Selected Row Values from Gridview in Asp.net</title>
      </head>
      <body>
          <form id="form1" runat="server">
              <div>
                  <asp:GridView ID="gvDetails" DataKeyNames="UserId" AutoGenerateColumns="false" CellPadding="5" runat="server">
                      <Columns>
                          <asp:TemplateField>
                              <ItemTemplate>
                                  <asp:CheckBox ID="chkSelect" runat="server" />
                              </ItemTemplate>
                          </asp:TemplateField>
                          <asp:BoundField HeaderText="UserId" DataField="UserId" />
                          <asp:BoundField HeaderText="UserName" DataField="UserName" />
                          <asp:BoundField HeaderText="Education" DataField="Education" />
                          <asp:BoundField HeaderText="Location" DataField="Location" />
                      </Columns>
                      <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                  </asp:GridView>
                  <asp:Button ID="btnProcess" Text="Get Selected Records" runat="server" Font-Bold="true" onclick="btnProcess_Click" />
                  <br />
                  <asp:Label ID="lblmsg" runat="server" />
              </div>
          </form>
      </body>
      </html>

      后面的代码:这只是为了将 GridView 与正确的数据绑定

      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              BindGridviewData();
          }
      }
      
      protected void BindGridviewData()
      {
          DataTable dt = new DataTable();
          dt.Columns.Add("UserId", typeof(Int32));
          dt.Columns.Add("UserName", typeof(string));
          dt.Columns.Add("Education", typeof(string));
          dt.Columns.Add("Location", typeof(string));
      
          DataRow dtrow = dt.NewRow();        //Create New Row
          dtrow["UserId"] = 1;                //Bind Data to Columns
          dtrow["UserName"] = "SureshDasari";
          dtrow["Education"] = "B.Tech";
          dtrow["Location"] = "Chennai";
          dt.Rows.Add(dtrow);
      
          dtrow = dt.NewRow();               //Create New Row
          dtrow["UserId"] = 2;               //Bind Data to Columns
          dtrow["UserName"] = "MadhavSai";
          dtrow["Education"] = "MBA";
          dtrow["Location"] = "Nagpur";
          dt.Rows.Add(dtrow);
      
          dtrow = dt.NewRow();              //Create New Row
          dtrow["UserId"] = 3;              //Bind Data to Columns
          dtrow["UserName"] = "MaheshDasari";
          dtrow["Education"] = "B.Tech";
          dtrow["Location"] = "Nuzividu";
          dt.Rows.Add(dtrow);
          
          gvDetails.DataSource = dt;
          gvDetails.DataBind();
      }
      

      Button_Click事件的代码

      protected void btnProcess_Click(object sender, EventArgs e)
      {
          string str = string.Empty;
          string strname = string.Empty;
          string edu = string.Empty;
          string location = string.Empty;
          foreach (GridViewRow gvrow in gvDetails.Rows)
          {
              CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
              if (chk != null & chk.Checked)
              {
                  //To Fetch the row index
                  //str += gvDetails.SelectedIndex.ToString();
                  
                  //To Fetch the value of Selected Row.
                  str += gvDetails.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
                  strname += gvrow.Cells[2].Text + ',';
                  edu += gvrow.Cells[3].Text + ',';
                  location += gvrow.Cells[4].Text + ',';
              }
          }
          str = str.Trim(",".ToCharArray());
          strname = strname.Trim(",".ToCharArray());
          lblmsg.Text = "Selected UserIds: <b>" + str + "</b><br/>" + "Selected UserNames: <b>" + strname + "</b><br>" + " Education: <b>" + edu + "</b><br>" + " Location: <b>" + location + "</b><br>";
      }
      

      【讨论】:

        【解决方案3】:
        protected void Button1_Click(object sender, EventArgs e)
        {
                int count= 0;
                foreach (GridViewRow gvindex in GridView1.Rows)
                {
                    CheckBox chck = gvrow.FindControl("CheckBoxG1") as CheckBox;
                    if (chck.Checked)
                    {
                        GridView1.EditIndex = count;
                        DataBind(); //Your Databind Function
                    }
                    count++;
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-03
          • 1970-01-01
          • 2010-11-10
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多