unable to find edit template control checkbox in row command

307 Views Asked by At

I tried to get the control of edit template which is checkbox in row command event but I am unable to get it, but I am getting label control which is in row index.

I tried the above below code to get the control:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);

    Label icllbl = (Label)GridView1.Rows[gvr.RowIndex].FindControl("icllbl");
    CheckBox iclcb = (CheckBox)GridView1.Rows[gvr.RowIndex].FindControl("iclcb");

    if (e.CommandName.Equals("Edit"))
    {                
        if (icllbl.Text == "Y")
        {
            iclcb.Checked = true;
        }

    }
}

And I tried RowDataBound event also luckily I am getting checkbox control here but this time I am unable to get the Label control in below code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label icllbl = (Label)e.Row.FindControl("icllbl");

        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {

            CheckBox iclcb = (CheckBox)e.Row.FindControl("iclcb");
            if (icllbl.Text == "Y")
            {
                iclcb.Checked = true;
            }
        }
    }
}

Please correct me if I am wrong anywhere.

Thanks in advance!

1

There are 1 best solutions below

1
AudioBubble On

In your RowCommand event use control class to cast into GridViewRow:

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int rowIndex = row.RowIndex;

And in RowDataBound event place Label control inside Edit (check for EditTemplate) check:

if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
    Label icllbl = (Label)e.Row.FindControl("icllbl");
    CheckBox iclcb = (CheckBox)e.Row.FindControl("iclcb");

    //... other code of lines will be here
}