0

My intention is diseable RequiredFieldValidator in grid view row. To find this validator i'am using FindControl method. I wrote this code:

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditRow")
    {
        int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
        GridView2.EditIndex = rowIndex;
        RequiredFieldValidator field46 = ((RequiredFieldValidator)GridView2.Rows[rowIndex].FindControl("RequiredFieldValidator46"));
        field46.Visible = false;
        ZaladujGridView();
    }
}

but when i press on "Edit" link button on grid view i'am receiving error "object reference not set to an instance of an object" in this line:

field46.Visible = false;

Can You help me with that ?.

Shagohad
  • 361
  • 1
  • 10
  • 20

1 Answers1

0

Validators are invisible until the validation fails (ie, a required field is empty). Since you are setting the Validator to invisible, the validator is not being rendered and that may be the reason why the control can't be found.

See the answer here: asp.net requiredfieldvalidator dont work when visible=false

Have you tried CausesValidation="false"? What else have you tried?

Community
  • 1
  • 1
Dan Romano
  • 73
  • 1
  • 1
  • 9