1

I have a gridview, and I made a template column with a checkbox inside. Then I want to check the value of checkboxes. I'm trying to set the row's visible property to false when that row's checkbox is unselected. I'm always getting null, no matter what I do.

It must be a problem with the FindControl(), but I think it is perfectly normal:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DbInteract dbi = new DbInteract("CONNECTION STRING");
        GridView1.DataSource = dbi.SqlDA("select * from table");
        GridView1.DataBind();
    }
    protected void ProsseguirBtn_Click(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chk");
                if (!cb.Checked)
                {
                    GridView1.Rows[row.RowIndex].Visible = false;
                }
            }
        }
    }
}

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

<!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="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chk" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="nome" HeaderText="jhf" />
            </Columns>
        </asp:GridView>
        <asp:Button ID="ProsseguirBtn" runat="server" Text="Button" 
            onclick="ProsseguirBtn_Click" />

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    </div>
    </form>
</body>
</html>
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85

2 Answers2

0

FindControl is not recursive. In other words, when you call FindControl on your row, it looks at only the immediate controls that the row contains.

A GridViewRow doesn't directly contain your controls - it contains table cells, which then contain your controls. So FindControl won't find your checkbox.

You'll need to use another method, like a foreach loop over the table cells if you don't know the column that you want, or write a recursive version of FindControl. You can find a version that I use sometimes in my old answer here.

Community
  • 1
  • 1
womp
  • 115,835
  • 26
  • 236
  • 269
0

Why do you require the check for !IsPostBack?

I have tried this code without this !IsPostBack check and the CheckBox is found correctly, otherwise IsPostBack is false and the code to find the CheckBox will not be hit.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Adam Nuttall
  • 477
  • 2
  • 16