1

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.

1
  • Either assign your data on Page_Load all the time or on every secondary event, i.e. grid sorting, page index change etc (anything that changes grid's state). See my updated answer...
    – Ruslan
    Apr 6, 2009 at 15:40

3 Answers 3

1

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.

1
  • Thanks! that does make sense and WORKED :) one thing to mention though, If Viewstate is enabled on the Grid it works better if the Data property is set in PreRender event as opposed to page load. However If ViewState is Disabled it works fine if the Data property is set in the PageLoad event!
    – eMTeeN
    Apr 6, 2009 at 16:12
1

It should work if you do your databinding on the PreRender event

Since the XML datasource is being set dynamically if you set it on the PageLoad all the page elements might not exist at this stage.

0
0

Are you implelenting the OnPageChanging Event ? Normally you need to implement it and use the e.NewPageIndex property from the Event Argument to set it in your gridview.

2
  • You don't need to do that if the data source is assigned declaratively, which I presume is the case...
    – Ruslan
    Apr 3, 2009 at 19:22
  • Btw, if the OnPageChanging wasn't implemented, it would give a different error, specifically stating that it was fired and not handled.
    – Ruslan
    Apr 3, 2009 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.