Methods and instances for transferring arbitrary data between ASP. NET pages

Source: Internet
Author: User

Recently, there have been many questions about passing parameters between different pages in the forum. I would like to give some help to my friends, and I hope my friends can carry out more discussions in this regard, you are welcome to propose a better method.

The common usage of passing parameters between different pages is as follows:
1. url-based Parameters
This method is subject to many restrictions. One is to expose parameters, which may lead to some security risks. The other is that if the number of included parameters is large, it will be difficult to control them;

2. Submit through post in a regular form
This method is most commonly used, but the from control in the ASP. NET page cannot pass parameters to another page as the general form does. It must be in a specific way, otherwise it is the same as the first method.

3. Session Mode
One of the traditional methods is advantageous for a specific environment and easier to release.

4. Implemented through interfaces or classes. We will mainly discuss this method.
In the web-based mode, private variables must be used for variable transmission, because it is very simple. If you use public or static variables, there will be great confusion. You can consider why?
If class attributes are used, one problem is that the survival time and range of objects cannot be transmitted in private mode.

The following is an example written using some methods in the SDK for your reference:

Note: To transfer a larger number and type of parameters, it is also a problem for chequan friends. We should consider using the structure to implement it. The structure has its own advantages. Reasonable Use should not cause too much resource occupation, especially in this transfer. If you have any ideas, you can also discuss the use of the structure.
Example:
Create a class file named trancfer. CS with the following content:
Using system;

Namespace weblog. Class
{
/// <Summary>
/// Define a structure. You can define the content to be passed and define multiple structures to suit your needs.
/// </Summary>
Public struct normaluserreginfo
{
Public String username;
Public String userpassword;
Public String quesion;
Public String answer;
Public String useremail;
Public String userclass;
}

/// <Summary>
/// This interface defines an attribute for returning the object to be passed
/// The page for sending objects must implement this interface
/// </Summary>
Public interface itransferface
{
Trancferclass tranceferobject {Get; set ;}
}

/// <Summary>
/// Summary of trancfer.
/// </Summary>
Public class trancferclass
{
Public trancferclass ()
{
//
// Todo: add the constructor logic here
//
}

Private normaluserreginfo _ strmessage;

Public normaluserreginfo trancefermessage
{
Get
{
Return this. _ strmessage;
}
Set
{
This. _ strmessage = value;
}
}

}
}

With this class, you can perform operations on the page,
Suppose we have a page named set. aspx, which uses a button to send some parameters to the get. ASPX page,
Related to the two pagesCodeAs follows:
================================
Set. aspx. CS
================================
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using weblog. Class;

Namespace Weblog
{
/// <Summary>
/// Set summary.
/// </Summary>
Public class set: system. Web. UI. Page, itransferface
{
Protected system. Web. UI. webcontrols. Button button1;
Private trancferclass _ temptrancefer;
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
_ Temptrancefer = new trancferclass ();
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

// Implement the itransferface Interface
Public trancferclass tranceferobject
{
Get
{
Return this. _ temptrancefer;
}
Set
{
This. _ temptrancefer = value;
}
}

Private void button#click (Object sender, system. eventargs E)
{
// Define a structure and pass it
Normaluserreginfo temptest = new normaluserreginfo ();
Temptest. Answer = "aaaaaaaaaaaaaaaa ";
Temptest. quesion = "bbbbbbbbbbbbbbb ";
Temptest. Username = "ccccccccccccccccc ";
Temptest. userpassword = "ddddddddddddddddddddddddddddd ";
This. tranceferobject. trancefermessage = temptest;

// Call server. Transfer () and turn to get. aspx
Server. Transfer ("Get. aspx ");
}
}
}

================================
Get. aspx. CS
================================
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using weblog. Class;

Namespace Weblog
{
/// <Summary>
/// Summary of the author.
/// </Summary>
Public class get: system. Web. UI. Page
{

Private void page_load (Object sender, system. eventargs E)
{
Itransferface mysender;

If (! Ispostback)
{
// Determine the httphandler of the current page. If the itransferface interface is implemented
// Convert to itransferface
If (context. handler is itransferface)
{
Mysender = (itransferface) Context. Handler;
Normaluserreginfo tempreceive = mysender. tranceferobject. trancefermessage;
Response. Write (tempreceive. Answer + "<br> ");
Response. Write (tempreceive. quesion + "<br> ");
Response. Write (tempreceive. username + "<br> ");
Response. Write (tempreceive. userpassword + "<br> ");
}
Else
{
Response. Write ("no parameters are received ");
}
}
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

}
}

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.