I am writing my very first asp.net application and am trying to use the PreviousPage property to access data between pages. Visual 2010 is giving me an error which I don’t understand, so I need some help to understand what I am doing wrong.
I have an application where I will bounce between all the pages using Transfer. That way, from a user perspective there is only ever one url that he sees. This url will be the one that makes him log in to the application (a application controlled function for now) and connect to the database. I therefore have an sqlclient.sqlconnection object that I wish to hand off to the next page called _dbConnection this is a private variable in my page class declared …
Partial Class Protocol
Inherits System.Web.UI.Page
Private _dbconnection As SqlClient.SqlConnection
Public ReadOnly Property dbConnection As SqlClient.SqlConnection
Get
Return _dbConnection
End Get
End Property
...
Later down the code, in reponse to a click event on a button
Server.Transfer("PSetup.aspx")
In PSetup.aspx I have the following
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="True" CodeFile="PSetup.aspx.vb" Inherits="PSetup" %>
<%@ PreviousPageType VirtualPath="~/Protocol.aspx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
Which should declare the Protocol class as the previous page
However in PSetup’s Page_Load Sub I attempt this
_dbConnection = PreviousPage.dbConnection
where in this use _dbConnection is a private variable in the new page class.
Visual Studio is giving me an error ‘dbConnection’ is not a member of ‘System.Web.UI.Page’
I have read through the documentation about this several times and I just don’t understand what I am doing wrong. Could someone help me please.
I guess the first thing you need to understand is that ASP.NET is stateless. That means that no data is “saved” between pages. What the
PreviousPageproperty does is it allows the request information persist through the use of aTransfermethod, and the request information will be the GET or POST variables.IMHO, the best way to do what you desire, is to have a shared public class that is not related to either page, and have a public function for getting the
SqlConnection. Remember though, it is stateless, so you will have to create the connection each time.Another alternative would be to save the dbConnection in the session (which I don’t recommend, since it is possible to view the session information).
Otherwise, you can’t have a variable persist with information between pages. Thats what is means to be stateless.
ALSO, for
PreviousPage, keep in mind that it is creating an instance ofSystem.Web.UI.Page, not an instance ofProtocol. That means public properties won’t exist fromProtocol, only native features ofPagewill be there.edit
From msdn
Note
Properties on the source page that are created primarily to expose values for cross-page posting are usually read-only properties. Although the source page can contain public read/write properties, setting a source page property from the target page property generally has no purpose, because the value will not be persisted.