Asp.net| Objects | page
We know that asp.net can be implemented with session when passing objects between different pages. Now I would like to introduce another method to achieve the goal.
Main idea: We need two pages, a sender.aspx is responsible for sending the object to be delivered, a receiver.aspx is responsible for accepting send over the object. Sender.aspx mainly used Server.Transfer to terminate the execution of the current page, and began to transfer the receiver.aspx to remove the object.
Specific implementation:
ITransfer.cs
Namespace Sunrise.Sample.FormTransfer
{
<summary>
This interface defines a property that returns the desired delivery object
The page that sent the object must implement this interface
</summary>
public interface Itransfer
{
MyClass MyObject
{
Get
Set
}
}
}
MyClass.cs
Namespace Sunrise.Sample.FormTransfer
{
<summary>
A simple class to demonstrate
</summary>
public class MyClass
{
private string _strmessage;
Public MyClass ()
{
}
public string Message
{
Get
{
return this._strmessage;
}
Set
{
This._strmessage = value;
}
}
}
}
Background code file Receiver.aspx.cs
Namespace Sunrise.Sample.FormTransfer
{
<summary>
The page that receives the object
</summary>
public class Receiver:System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{
Itransfer Mysender;
if (! IsPostBack)
{
To determine the HttpHandler of the current page, if the Itransfer interface is implemented
It's converted into itransfer.
if (Context.Handler is Itransfer)
{
Mysender = (itransfer) Context.Handler;
Response.Write (MySender.MyObject.Message);
}
Else
{
Response.Redirect ("sender.aspx");
}
}
}
.
.
.
}
}
Background code file Sender.aspx.cs
Namespace Sunrise.Sample.FormTransfer
{
<summary>
The page where the object is sent
</summary>
public class Sender:System.Web.UI.Page, Itransfer//Inheritance Itransfer
{
protected System.Web.UI.WebControls.Button btnsend;
Private MyClass _objmyobject;
private void Page_Load (object sender, System.EventArgs e)
{
_objmyobject = new MyClass ();
}
Implement Itransfer interface
Public MyClass MyObject
{
Get
{
return this._objmyobject;
}
Set
{
This._objmyobject = value;
}
}
<summary>
Responding to button events
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Btnsend_click (object sender, System.EventArgs e)
{
This. Myobject.message = "Test";
Call Server.Transfer () and move to execution receiver.aspx
Server.Transfer ("receiver.aspx");
}
.
.
.
}
}
After compiling, execute sender.aspx, click the Btnsend button, you can see the execution result: the receiver.aspx page got the MyObject object, and on the page the Myobject.message value is "test";
Need complete project source code please contact Author: szumic@hotmail.com