Asp.net context. Handler page value transfer method page 1/2

Source: Internet
Author: User

I. Currently, there are several methods for passing values on pages in ASP. NET:

1. Form submission,

<Form action = "target. aspx" method = "Post" name = "form1">

<Input name = "param1" value = "1111"/>

<Input name = "param2" value = "2222"/>

</Form>

....

Form1.submit ();

....

This method is implemented in ASP. Net is invalid because ASP. . Net forms are always submitted to their own pages. If you want to submit other pages, special processing is required.

2. <a href = "target. aspx? Param1 = 1111 & param2 = 2222 "> link transfer </a>

Receiving page: String STR = request ["param1"]

3. Session sharing

Sending page: Session ("param1") = "1111 ";

String STR = SESSION ("param1"). tostring ();

4. Application Sharing

Sending page: Application ("param1") = "1111 ";

By page: String STR = Application ("param1"). tostring ();

This method is not often used because the application is in an application Program Domain range sharing. All users can change and set their values, so they only apply counters and other places that require global variables.

5. Cookie

6. response. Redirect () method

Response. Redirect ("target. aspx? Param1 = 1111 & param2 = 2222 ")

Receiving page: String STR = request ["param1"]

7. server. Transfer () mode.

Server. Transfer ("target. aspx? Param1 = 1111 & param2 = 2222 ")

Receiving page: String STR = request ["param1"]

2. If you need to transmit a large number of parameters between two pages, such as data query pages, use the 1-6 method to transfer the value and the inconvenience, the 7th methods have a unique advantage! However, some settings are required to use this method. The following describes how to use this method:

Take the Data Query page as an example:

On the query page, set the following public attribute (querypage. aspx): Public class querypage: system. Web. UI. Page

{

Protected system. Web. UI. webcontrols. textbox txtstadate;

Protected system. Web. UI. webcontrols. textbox txtenddate;

/** // <Summary>

/// Start Time

/// </Summary>

Public String stadate

{

Get {return this.txt stadate. Text ;}

Setincluthis.txt stadate. Text = value ;}

}

/** // <Summary>

/// End Time

/// </Summary>

Public String enddate

{

Get {return this.txt enddate. Text ;}

Setincluthis.txt enddate. Text = value ;}

}

Private void btnenter_click (Object sender, system. eventargs E)

{

Server. Transfer ("resultpage. aspx"); // Note: Use resultpage. aspx to receive the passed parameters.

}

}

On the displayed query result page (resultpage. aspx ):

Public class resultpage: system. Web. UI. Page

{

Private void page_load (Object sender, system. eventargs E)

{

// Convert to obtain the data entered on the previous page.

Querypage = (querypage) Context. Handler; // Note: reference the page handle.

Response. Write ("stadate :");

Response. Write (querypage. stadate );

Response. Write ("<br/> enddate :");

Response. Write (querypage. enddate );

}

}

3. If many query pages share a result page, set the method as follows:

The key to this method is the conversion of "querypage = (querypage) Context. Handler;", which can be implemented only when the conversion does not depend on a specific page.

If you want all query pages to inherit an interface and define a method in this interface, the only function of this method is to obtain the parameters required for building the result on the result page, you can share multiple pages with one result page!

1. Define a class and place all query parameters with the class :(*. CS) /** //


// the value to be used on the result page
///

public class queryparams

{< br>
private string stadate;
private string enddate;

/** //


// start time
// /
Public String stadate

{< br>
get {return this. stadate ;}< br>
set {This. stadate = value ;}

}< br>
/** //
/// end Time
//
Public String enddate

{< br>
get {return this. enddate ;}< br>
set {This. enddate = value ;}< br>
}< br>
2. Interface Definition:
/* * ////
// define the query interface.
//
Public interface iqueryparams

{< br>
/**// ** // ** ///
// parameter
///

queryparams parameters {Get ;}

}< br>
3. the query page inherits the iqueryparams interface (querypage. aspx):
/** //
// query the page, inheritance interface
//
public class querypage: system. web. UI. page, iqueryparams

{< br>
protected system. web. UI. webcontrols. textbox txtstadate;
protected system. web. UI. webcontrols. textbox txtenddate;

Private queryparams;

/** // <Summary>

/// Parameters used on the result page

/// </Summary>

Public queryparams Parameters

{

Get

{

Return queryparams;

}

}

Private void btnenter_click (Object sender, system. eventargs E)

{

// Assign a value

Queryparams = new queryparams ();

Queryparams. stadate = this.txt stadate. text;

Queryparams. enddate = this.txt enddate. Text

Server. Transfer ("resultpage. aspx ");

}

}

4. This is also true for other pages.

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.