简体   繁体   English

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

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

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? 有人可以帮忙吗?

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

How do you populate your GridView? 你如何填充你的GridView? If you do this in Page_Load, make sure you are not doing it on postbacks (check IsPostBack). 如果您在Page_Load中执行此操作,请确保您没有在回发中执行此操作(请检查IsPostBack)。

Is your chkBx variable null? 你的chkBx变量是null吗?

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)
            {
                // ...
            }
        }
    }
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 核实

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;

Jakob Answer will work if below line is used. 如果使用以下行,Jakob Answer将起作用。 Even one control only in the cell, index need to be 1 not 0 即使只有一个控件在单元格中,索引也需要为1而不是0

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

Thank you Sam 谢谢山姆

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

相关问题 如何从asp.net中的多个选中的复选框中获取值? - How to get values from multiple checked checkboxes in asp.net? 如何在asp:Repeater中的asp:CheckBoxList中获取所有选中的复选框 - How to get all checked checkboxes in an asp:CheckBoxList inside an asp:Repeater 在asp.net GridView中选中的复选框 - Checkbox checked inside asp.net GridView 如何使用带有asp.net的javascript在gridview中检查行的单元格值 - How to get checked rows cell value in a gridview using javascript with asp.net ASP.NET-如何一次检查gridview页面中的所有复选框,并访问页面中gridview中选中的行? - ASP.NET - How can I check all checkboxes across gridview pages at once and access checked rows in a gridview across pages? 在ASP.NET中控制Gridview行内的复选框 - Controlling Checkboxes inside a Gridview Row in ASP.NET 如何在ASP.NET上绑定的GridView内的文本框中保存值? - How to keep values in textbox inside a binded GridView on ASP.NET? 如何在TemplateField asp.net中访问gridview值 - how to access the gridview values inside the TemplateField asp.net 如何在WPF Gridview中获取检查的行值 - How to get checked row values in WPF Gridview 如何使用c#asp.net在LinkBut​​ton的click事件中获取gridview的DataKeyNames值 - How to get DataKeyNames value of gridview inside LinkButton's click event using c# asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM