locked
NullException when trying to hide and show checkbox within gridview RRS feed

  • Question

  • User2129219356 posted

    Hi,

    I am new to VB and im having issues hiding and showing a check box with a gridview. I am being displayed with the error 'NullReferenceException was unhandled by user code' at the line of the code 'Chk.Visible = False'

    If Me.rblDisplay.SelectedValue = "HISTORY" Then

    Dim Chk As CheckBox = DirectCast(grdResults.FindControl("CheckBox_Delete"), CheckBox)

                        Chk.Visible = True

    Else     

    Dim Chk As CheckBox = DirectCast(grdResults.FindControl("CheckBox_Delete"), CheckBox)

                        Chk.Visible = False

    End If

    Any help would be great

    Tuesday, March 4, 2014 5:09 AM

Answers

  • User-1716253493 posted
    To get control inside Gridview row you need to specify which row. GridView1.Rows(RowIndex).FindControl("x")
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 4, 2014 7:51 AM
  • User-1818759697 posted

    Hi,

    grdResults.FindControl("CheckBox_Delete")

    For this situation, the above codes couldn't get the dropdownlist. And you could refer to the following information:

    GridView is a control which repeats its rows based on the bound data, e.g in practise it creates a set of controls (one row in it) as many times as there are rows in data source. In order to keep IDs of these controls unique, GridView and its rows are naming containers (implement INamingContainer interface). Being a naming container means that a control provides a new naming scope for its child controls by appending it's ID into ID of its own naming container.

    And being a naming container means that in order to look for a control with its local ID (not the unique id) you need to run FindControl against its naming container. In this case you run it againt GridView but GridView is not the direct naming container (or parent) of the HiddenField but the corresponding row (GridViewRow) is.

    (DropDownList)GridView1.Rows[i].FindControl("DropDownList1")

    Besides, you could also refer to:

    http://www.codeproject.com/Articles/12021/Accessing-the-different-controls-inside-a-GridView

    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 4, 2014 10:12 PM

All replies

  • Tuesday, March 4, 2014 5:13 AM
  • User-1716253493 posted
    To get control inside Gridview row you need to specify which row. GridView1.Rows(RowIndex).FindControl("x")
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 4, 2014 7:51 AM
  • User-1818759697 posted

    Hi,

    grdResults.FindControl("CheckBox_Delete")

    For this situation, the above codes couldn't get the dropdownlist. And you could refer to the following information:

    GridView is a control which repeats its rows based on the bound data, e.g in practise it creates a set of controls (one row in it) as many times as there are rows in data source. In order to keep IDs of these controls unique, GridView and its rows are naming containers (implement INamingContainer interface). Being a naming container means that a control provides a new naming scope for its child controls by appending it's ID into ID of its own naming container.

    And being a naming container means that in order to look for a control with its local ID (not the unique id) you need to run FindControl against its naming container. In this case you run it againt GridView but GridView is not the direct naming container (or parent) of the HiddenField but the corresponding row (GridViewRow) is.

    (DropDownList)GridView1.Rows[i].FindControl("DropDownList1")

    Besides, you could also refer to:

    http://www.codeproject.com/Articles/12021/Accessing-the-different-controls-inside-a-GridView

    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 4, 2014 10:12 PM