genericHCU
genericHCU

Reputation: 4446

What is the proper way to use data from a grid view before data binding

I have a user control that creates a grid view of database records. Each record has a checkbox for deleting the record.

My problem is that the the page_load event builds the grid view, then the delete button even fires. The deleteButton_click event is looping over the gridview looking for checked boxes but never finds any because the 'page_load' event just gave me a clean gridview. What is the best way to check for checked boxes before the grid view is re-built? Or can I get the checked values without looking at the grid view?

Protected Sub Page_Load(...) Handles Me.Load

    'db calls and other code
    gv.DataBind()
End Sub

Protected Sub btnDelAtt_Click(...) Handles btnDelAtt.Click

    For Each grdRow As GridViewRow In gvFileViewer.Row
        Dim chkBox As CheckBox = CType(grdRow.FindControl("cbItem"), CheckBox)
        If chkBox.Checked = True Then 'this is always false thanks to page_load
            'code that does not run
        end if
    next
end sub

Upvotes: 0

Views: 81

Answers (1)

Moe
Moe

Reputation: 1599

As mentioned in the comments, adding !IsPostBack should do it. You only need to load the grid from the database in the initial call, you don't need to get the data again when post back occurs. You will need to rebind the grid once the delete is over.

     Protected Sub Page_Load(...) Handles Me.Load
        If(!Page.IsPostBack)

            'db calls and other code
            gv.DataBind()

        End Sub

        Protected Sub btnDelAtt_Click(...) Handles btnDelAtt.Click

            For Each grdRow As GridViewRow In gvFileViewer.Row
                Dim chkBox As CheckBox = CType(grdRow.FindControl("cbItem"), CheckBox)
                //Delete your record
                end if

            next
            //Rebind grid
        end sub

Upvotes: 1

Related Questions