Asp. NET pages to pass objects between the alternative methods

Source: Internet
Author: User
Tags execution implement
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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.