I have a data grid populated with a dataset and it has paging enabled. i have set the onclick event and it then goes to the next page but when i then click the previous page button it generates the following.
Server Error in '/' Application.
--------------------------------------------------------------------------------
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.]
System.Web.UI.Control.LoadViewStateRecursive(Objec t savedState) +189
System.Web.UI.Control.LoadViewStateRecursive(Objec t savedState) +414
System.Web.UI.Control.LoadViewStateRecursive(Objec t savedState) +414
System.Web.UI.Control.LoadViewStateRecursive(Objec t savedState) +414
System.Web.UI.Control.LoadViewStateRecursive(Objec t savedState) +414
System.Web.UI.Control.LoadViewStateRecursive(Objec t savedState) +414
System.Web.UI.Control.LoadViewStateRecursive(Objec t savedState) +414
System.Web.UI.Page.LoadPageViewState() +306
System.Web.UI.Page.ProcessRequestMain() +423
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
my code is:
<%@ Page Language="
VB" Debug="True" %>
<script runat="server">
'This binds the information to the data grid
Sub Page_Load()
'This checks to see if their is a cookie on the viewers system.
If Request.Cookies("DJUserNameCookie") is nothing Then
'If there is not one then it sends the person back to the login page.
Response.Redirect("login.aspx")
End If
GridMessages.Datasource() = GetMessages()
GridMessages.Databind()
End Sub
'This is the function that calls for the messages that can then be bound in the page
'load event.
Function GetMessages() As System.Data.DataSet
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Inetpub\wwwroot\djguestbook\guestDatabas e.mdb"
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString )
Dim queryString As String = "SELECT [Messages].* FROM [Messages]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
'This is the function that controls the paging when the next buttons are clicked
Sub NewPage(sender As Object, e As DataGridPageChangedEventArgs)
GridMessages.CurrentPageIndex = e.NewPageIndex
GetMessages()
End Sub
'This is the sub event for everytime the edit button is clicked.
Sub btnEdit_Clicked(Sender as object, e as DataGridCommandEventArgs)
End Sub
</script>
<html>
<head>
<link media="screen" href="../StyleSheet.css" type="text/css" rel="stylesheet" />
</head>
<body background="../images/bg.jpg">
<div align="center">
<form runat="server">
<table cellspacing="0" cellpadding="0" width="1003" border="0">
<tbody>
<tr>
<td valign="top" background="../images/title.jpg" height="100">
<table cellspacing="0" cellpadding="0" width="1003" border="0">
<tbody>
<tr>
<td valign="bottom" align="middle" height="88">
Log Out</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="middle">
<br />
<asp:DataGrid id="GridMessages" runat="server" BorderColor="White" BorderWidth="1px" AutoGenerateColumns="False" AllowPaging="True" BorderStyle="None" CellPadding="3" PageSize="5" OnPageIndexChanged="NewPage">
<HeaderStyle backcolor="Gray"></HeaderStyle>
<PagerStyle borderwidth="1px" bordercolor="Black" borderstyle="Solid" backcolor="Gray"></PagerStyle>
<Columns>
<asp:TemplateColumn Visible="False" HeaderText="ID">
<ItemStyle width="125px"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("ID") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name">
<ItemStyle width="125px"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("GuestName") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Email">
<ItemStyle width="125px"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("GuestEmail") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Homepage">
<ItemStyle width="125px"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("GuestHomepage") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Message">
<ItemStyle width="250px"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("GuestMessage") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Edit" ButtonType="PushButton"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<br />
</td>
</tr>
</tbody>
</table>
<br />
Copyright 2004 David Jenkins.
</form>
</div>
</body>
</html>
cheers for any help.....
David