C# 如何在使用asp .net检查的gridview中获取CheckBoxes的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/540304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:22:01  来源:igfitidea点击:

How to get values of CheckBoxes inside a gridview that are checked using asp .net

c#

提问by

I am using checkbox in gridview .... I am using it in 1st cell.... When I select the checkbox at run time, I need to get those values... but on selecting or on click to checkbox, it's not finding or value is taking as FALSE... how to write in asp.net backend and in c# code?

我在 gridview 中使用复选框......我在第一个单元格中使用它......当我在运行时选择复选框时,我需要获取这些值......但是在选择或单击复选框时,它不是发现或值被视为错误...如何在 asp.net 后端和 c# 代码中编写?

 <asp:TemplateField>
   <ItemTemplate >
       <asp:checkbox id="ShowAddress" runat="server" />
  </ItemTemplate>
 </asp:TemplateField>

Code-behind:

代码隐藏:

  protected void Button1_Click(object sender, EventArgs e)
    {
        // Looping through all the rows in the GridView

        foreach (GridViewRow di in GridView1.Rows)
        {
         CheckBox chkBx = (CheckBox)di.FindControl("ShowAddress");

            if (chkBx != null && chkBx.Checked)
            {
                /// put your code here
            }
        }
    }

Is there any implementation to be done in script at page load?

页面加载时是否有任何要在脚本中完成的实现?

Can anyone help?

任何人都可以帮忙吗?

回答by Jakob Christensen

How do you populate your GridView? If you do this in Page_Load, make sure you are not doing it on postbacks (check IsPostBack).

你如何填充你的 GridView?如果您在 Page_Load 中执行此操作,请确保您没有在回发中执行此操作(检查 IsPostBack)。

Is your chkBx variable null?

您的 chkBx 变量是否为空?

The following code works:

以下代码有效:

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
            if (chk != null && chk.Checked)
            {
                // ...
            }
        }
    }

回答by USA

StringCollection idCollection = new StringCollection();
string strID = string.Empty;

for (int i = 0; i < GridView1.Rows.Count; i++)
{
   CheckBox chkDelete = (CheckBox) GridView1.Rows.Cells[0].FindControl("chkSelect");
   if (chkDelete != null)
   {
     if (chkDelete.Checked)
      {
          strID = GridView1.Rows.Cells[1].Text;
         idCollection.Add(strID);
    }
  }
} 

for more details check this link: http://www.itworld2.com/ghowto.aspx?id=69

有关更多详细信息,请查看此链接:http: //www.itworld2.com/ghowto.aspx?id=69

回答by Fakurdeen

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
     Loadgridview();// its a correct
    }//            not Loadgridview() here if you load above error is occur
 }

check it

核实

回答by Vaibhav Saran

int i = 0;
foreach (GridViewRow row in GridView1.Rows)
{
    CheckBox chk = (CheckBox)GridView_AdminTags.Rows[i].Cells[0].FindControl("chkTag");
    if (chk != null)
        if (chk.Checked)
        {
            ////.......;
        }
    i++;
}
i = 0;

回答by user3814410

Jakob Answer will work if below line is used. Even one control only in the cell, index need to be 1 not 0

如果使用以下行,Jakob Answer 将起作用。即使一个控件只在单元格中,索引也需要为 1 而不是 0

CheckBox chk = row.Cells[0].Controls[1] as CheckBox;

Thank you Sam

谢谢山姆