asp.netvb.netgridviewviewstatepage-index-changed

Gridview PageIndexChanging event not firing with ViewState off and Dynamic datasource


I am having issues getting my PageIndexChanging event to fire. Viewstate is switched off and I have a dynamic datasource which I am loading on each postback on PageInit event.

<asp:GridView   runat="server" 
                        ID="gvBenefitsList"
                        AllowPaging="true" 
                       >
              .....
                <PagerSettings Mode="NumericFirstLast" FirstPageText="<< First" LastPageText="Last >>"
                PageButtonCount="5" Position="Bottom" />
        </asp:GridView>

Index change event

Protected Sub gvBenefitsList_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvBenefitsList.PageIndexChanging
        gvBenefitsList.PageIndex = e.NewPageIndex
        gvBenefitsList.DataSource = Data
        gvBenefitsList.DataBind()
    End Sub

I have to rebind the grid every time as we have viewstate switched off.

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Data = getdata()
        gvBenefitsList.DataSource = Data
        gvBenefitsList.DataBind()
    End Sub

Oh and Data is a DataSet property

 Dim m_Data As DataSet
    Public Property Data As DataSet
        Get
            Return m_Data
        End Get
        Set(value As DataSet)
            m_Data = value
        End Set
    End Property

So when clicking page 2 the init event fires and binds the grid but the index change event never gets fired. I've tired programatically attaching the handler in the init event

 AddHandler gvBenefitsList.PageIndexChanging, AddressOf gvBenefitsList_PageIndexChanging

but that didn't make a difference. I'm unsure as to why this is not working as rowCommand event is working fine. This is part of a master page with Viewstate off and we go as far as setting the hidden Viewstate variables to an empty string to cut down on the page size.

 <input type="hidden" name="__EVENTTARGET" value="" />

Any ideas would be welcome.


Solution

  • So I seem to have found the culprit. As stated in the master page we are removing the values for

    <input type="hidden" name="__EVENTTARGET" value="" />
    <input type="hidden" name="__EVENTARGUMENT" value="" /> 
    

    This seems to cause an issue with certain javascript postback events. I found a really good article here saying what they do.

    http://aspalliance.com/895_Understanding_the_JavaScript___doPostBack_Function.all

    These two hidden fields control any linkbuttons that do a JavaScript___doPostBack call but do not affect normal buttons or image buttons as they do a proper postback to the server.

    Checking the post I can see that when clicking the paging link the values are:

    __EVENTARGUMENT - Page$2
    
    __EVENTTARGET - ctl00$MasterContent$gvBenefitsList
    

    Even though viewstate is switched off these fields are still used for these events.

    Hope this helps somebody out. I've been struggling with it for a while!