Alternative ways to pass objects between ASP.NET pages

zhaozj2021-02-16  47

We know that ASP.NET can be implemented with session when passing objects between different pages. Now I want to introduce another method to achieve the purpose.

Main ideas: We need two pages, a Sender.aspx is responsible for sending the object you want, and a Receiver.aspx is responsible for accepting the SEND. Sender.aspx mainly uses Server.Transfer to terminate the execution of the current page and start to transfer to Receiver.aspx to remove the object.

Implementation:

Itransfer.cs namespace sunrise.sample.formtransfer {///

/// This interface defines a page that returns the property /// send object of the required delivery object must be implemented /// public Interface itransfer {myclass myObject {get; set;}}}

Myclass.csnamespace sunrise.sample.formtransfer {///

/// A simple class /// public class myclass {private string _StrMessage;

Public myclass () {}

Public string message {get {return;} set {this._strime = value;}}

}

Backstage code file receiver.aspx.cs

Namespace Sunrise.Sample.FormTransfer {///

/// Receive object page /// public class receiver: system.Web.ui.page {private void Page_load (Object Sender, System.Eventargs E ) {Itransfer mysender;

if (! ispostback) {// Judging the httphandler of the current page, if the ITRANSFER interface is implemented into iTransfer if (CONTEXT.HANDAL ITRANSFER) {MySender = (Itransfer) Context.handler;

Response.write (MySender.MyObject.Message);} else {response.redirect ("sender.aspx");}}}.

Backstage code file sender.aspx.cs

Namespace Sunrise.Sample.FormTransfer {///

/// Send object's page /// public class sender: system.web.ui.page, itransfer // Inherited 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;}} ///

/// response button event /// /// < Param name = "sender"> /// private void btnsend_click (object sender, system.eventargs e) {this.myObject.Message = "test";

// Call server.transfer (), turn to execute 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 showing the value of myObject.Message on the page; Test ";

Project requires complete source code please contact the author: szumic@hotmail.com http://sunrise.szu.edu.cn

转载请注明原文地址:https://www.9cbs.com/read-27298.html

New Post(0)