views:

56

answers:

3

My problem is the following:

I have a gridview , and I made a template column with a checkbox inside. Then , obviously , I want to check the value of checkboxes . I'm trying to set rows' visible property to false when that row's checkbox is unselected. I'm always getting null , no matter what I do . So , 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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>

Any help would be highly apreciated . :x Thanks in advance .

A: 

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.

womp
A: 

Hi,

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.

ginozola
A: 

I do not require . The result is pretty much the same , with or without . At least for me here .

I tried womp's sollution, does not work too :(

Can't really find what's wrong .


Problem solved :D

Framework 4 BETA 2 -.- Don't ever use this f' shit .

lglifesgood