R – Paging Problems With Standard .net 2.0 Gridview using VB.Net

.net-2.0asp.netgridviewpagingvb.net

I am using a standart .net 2.0 Gridview which uses an XMLDatasource to populate the Grid. The Data property of the XMLDatasource is set dynamically which allows the gridview to change based on input.

All this works fine however I am having problems with paging…

I have set the AllowPaging Property to "true" and set the PageSize Property to "10". The GridView populates fine the first time around showing the first 10 records and the number of pages as hyperlinks at the bottom, BUT when i try to click on any of the page numbers to view them a message box pops up saying "Object reference not set to an instance of an object"

any ideas what I'm doing wrong?? or is there anything i need to do which i have missed out on??

Code currently being used;

Gridview…

<asp:GridView ID="GridView1" 
      Runat="server" 
      DataSourceID="XmlDataSource1" 
      AutoGenerateColumns="False" 
      AllowPaging="True"
      style="width:100%; height:100%;"  
      EnableViewState="False">
<SelectedRowStyle BackColor="Red" />
<Columns>
   <asp:BoundField DataField="TYPE" HeaderText="TYPE" SortExpression="TYPE" />
   <asp:BoundField DataField="DESCRIPTION" HeaderText="DESCRIPTION" SortExpression="DESCRIPTION" />
</Columns>
</asp:GridView>

XMLDatasource…

<asp:XmlDataSource ID="XmlDataSource1" runat="server" TransformFile="~/XML/grid2.xslt" EnableCaching="False">
</asp:XmlDataSource>

vb.net code which sets the Data property of the XMLDatasource…

Private Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click

  XmlDataSource1.Data = _testLib.GetGridXML(_Num)
  GridView1.DataBind()
End Sub

where _testLib.GetGridXML is a function that returns an XML string based on the _Num passed in.

Best Answer

It's difficult to say without seeing your code... I would speculate that you assign the Data conditionally, i.e:

If Not IsPostBack Then
   MyXMLDataSource.Data = "...some xml..."
End If

In this case it will be empty on post back and you get your exception. Could be something else, but then again, no code...

Update

Since you've added more information...

You must have something like code above on Page_Load. Since you are not providing it here, I presume you do. If you don't, you'd get the null reference exception on each load.

With that in mind, you assign data on some button click, but not on PageIndexChanging.

You click the button, the page loads, you assign the data, the grid shows it. Then you click the grid's next link, the page loads again, PageIndexChanging gets fired, your click event doesn't -- where's assignment then?

From what I see, either assign the Data property on Page_Load every time or do it in all subsequent events, i.e. on page change, on sort, etc.

Btw, you don't have to call DataBind when assigning XmlDataSource declaratively.

Related Topic