使用 VB.Net 的标准 .net 2.0 Gridview 的分页问题
我正在使用标准的 .net 2.0 Gridview,它使用 XMLDatasource 来填充网格。 XMLDatasource 的 Data 属性是动态设置的,它允许 gridview 根据输入进行更改。
所有这些工作正常,但我在分页时遇到问题...
我已将AllowPaging 属性设置为“true”并将PageSize 属性设置为“10”。 GridView 第一次填充得很好,在底部显示前 10 条记录和页数作为超链接,但是当我尝试单击任何页码来查看它们时,会弹出一个消息框,显示“对象引用未设置”到一个对象的实例”
任何想法我做错了什么? 或者有什么我需要做但我错过了的事情吗?
当前正在使用的代码;
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>
设置 XMLDatasource 的 Data 属性的 vb.net 代码...
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
其中 _testLib.GetGridXML 是一个函数,它根据传入的 _Num 返回 XML 字符串。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有看到您的代码,很难说...我推测您是有条件地分配数据,即:
在这种情况下,回发时它将为空,并且您会得到异常。 可能是其他内容,但话又说回来,没有代码...
更新
由于您添加了更多信息...
您在 Page_Load 上必须有类似上面的代码。 既然您没有在这里提供,我想您已经提供了。 如果不这样做,每次加载时都会出现空引用异常。
考虑到这一点,您可以在单击某个按钮时分配数据,而不是在 PageIndexChanging 上分配数据。
单击按钮,页面加载,分配数据,网格显示它。 然后,您单击网格的下一个链接,页面再次加载,PageIndexChanging 被触发,但您的单击事件没有 - 那么分配在哪里?
据我所知,要么每次在Page_Load上分配Data属性,要么在所有后续事件中执行它,即在页面更改、排序等时。
顺便说一句,你不需要以声明方式分配 XmlDataSource 时不必调用 DataBind。
It's difficult to say without seeing your code... I would speculate that you assign the Data conditionally, i.e:
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.
如果您在 PreRender 事件上进行数据绑定,它应该可以工作,
因为如果您在 PageLoad 上设置 XML 数据源,则动态设置 XML 数据源,此时所有页面元素可能不存在。
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.
您正在实现 OnPageChanging 事件吗? 通常,您需要实现它并使用事件参数中的 e.NewPageIndex 属性在 gridview 中设置它。
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.