2

I have GridView with 2 columns.

The first column is: test-label (TemplateField)

The second: checkbox (asp:CheckBoxField) that connect to sql table with bit column (done).

I want that on page load - the page will check every row, where the checkbox = true, the test-label.visble will be false.

I know how to write code with SELECT statement to check the value from the SQL table, but don't know how to check every row on the gridview on the page-load.

how can I do that?

(i can't use findcontroll for the checkbox because it's checkboxfield and not just "checkbox".

<asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" /> 

so, what can I do here? maybe to replace that field with regular cb? (i don't know how to do there databind - on the regular cb).

2 Answers 2

2

you can use GridView.RowDataBound Event

so you can do something like

 protected void GVRowDataBound(object sender, GridViewRowEventArgs e)
        {
            var check = (CheckBox) e.Row.FindControl("ID"); // ID is id of the checkbox
            var lable = (Label) e.Row.FindControl("LableID");
            if(check != null && lable != null)
            {
                if(check.Checked)
                {
                    lable.Visible = false;
                }
            }
         }
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for null checking...I still think it would be good to check e.Row.RowType just for posterity.
the CheckBoxField dont have id atribute. i'm using: <asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" /> what i can to do for the findcontrol ?
i've found the solution.. simple.. just to do regular cb with Eval and your code is perfect. thank you.
2

You can't do it in Page.Load because the GridView isn't databound yet.

Try handling GridView.RowDataBound.

Code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox cb = (CheckBox)e.Row.FindControl("checkbox");
        Label lbl = (Label)e.Row.FindControl("test-label");
        lbl.Visible = !(cb.Checked);
    }
}

1 Comment

the CheckBoxField dont have id atribute. i'm using: <asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" /> what i can to do for the findcontrol ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.