Let's start with a simple gridview with xml file without any codings.
Students.xml
<students>
<student id="1" name="Richard">
<student id="2" name="Jacob">
<student id="3" name="Emily">
<student id="4" name="Michael">
<student id="5" name="Isabella">
<student id="6" name="Ethan">
<student id="7" name="Emma">
<student id="8" name="Joshua">
<student id="9" name="Ava">
<student id="10" name="Christopher">
<student id="11" name="Daniel">
<student id="12" name="Madison">
</student>
test.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="gv_Test" %>
<form id="form1" runat="server">
<asp:gridview id="GridView1" runat="server" allowpaging="True"
datasourceid="XmlDataSource1" />
<asp:xmldatasource id="XmlDataSource1" runat="server"
datafile="~/gv/Students.xml" />
</form>
Everything worked as expected, please notice the paging worked fine also.
Now it is the time to reproduce the error, removing datasourceid="XmlDataSource1" from gridview tag, and adding the following to the test.aspx.vb page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
GridView1.DataSource = XmlDataSource1
GridView1.DataBind()
End If
End Sub
It should work, right? Yes, the first page is, but if you click the page index, you will get the error
The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.
But if you replace
GridView1.DataSource = XmlDataSource1
with
GridView1.DataSourceID = "XmlDataSource1"
page index backs to work again. Interesting, isn't it?
I think the reason is the framework can not remember the DataSource object, but it has no problem for the string.
As you know, you can post the following to make the paging works again.
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
GridView1.DataSource = XmlDataSource1
GridView1.DataBind()
End Sub
But why? And how about allowing sorting? I am still doing research...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment