asp.net page parameters (parent page Fetch array table, etc.)

Source: Internet
Author: User

1.querystring Way: Write the required values directly to the back of the link, which will be displayed directly to the

In the address bar, one or more values that are not high on the delivery security requirement or that are simple in structure can make

In such a way.

such as: Response.Redirect ("Target.aspx?param1=hello?m2=hi")

The received page can get the value passed by request: string str =

request.querystring["param1"];

2.cookie way, using the cookie object method, the cookie is placed on the client's

Set Cookie:httpcookie cookie_name = new HttpCookie ("name");

Cookie_name. Value = Label1.Text;

Reponse.appendcookie (Cookie_name);

Get cookies:

String Name= Request.cookie

[' Name ']. Value.tostring ();

3.session variables, the session is placed on the server side of the

Set session:session["Name" = "Hello";

Gets the session:string name = session["Name". ToString ();

General comparison advocated with the session security is high, if the page needs to pass a lot of variables can be used

Hashtable or arrays save values and save them to the session.

4. Using Application Object variables

The scope of the Application object is the entire global, which means it works for all users. This method

Not often, because application is shared across an application domain, all users can change and set

Place its value, so only the use of counters, such as the need for global variables.

Set application:application["name" = = "Hello";

Gets the application:string name = application["Name". ToString ();

5. PostBackUrl () method

Default.aspx page:

Code

<asp:button id= "Button1" runat= "Server" text= "Posttoanotherpage"

Postbackurl= "~/default2.aspx"/>

Default2.aspx page:

Code

if (previouspage!= null)

{

TextBox TextBox1 = (textbox) Previouspage.findcontrol

("TextBox1");

Response.Write (TextBox1.Text);

}、

6. Using the Server.Transfer method

This can be said to be the surface of the object of the development of the method used, its use of Server.Transfer method to

The process is directed from the current page to another page, and the new page uses the response stream from the previous page, so

This method is completely object-like, concise and effective. The following code is shown in the need for many parameters

, the use of the method, if the parameters are less necessary to use this method.

If you want all query pages to inherit an interface and define a method in the interface, the method's

A function is to let the results page to obtain the construction results of the required parameters, you can achieve multiple pages sharing a result

Page operation.

1, first define a class, use this class to place all query parameters:

Code

/**////<summary>

Summary description of Queryparams

</summary>

public class Queryparams

{

private string FirstName;

private string LastName;

private int age;

public string Firstname

{

get {return this.firstname;}

set {this.firstname = value;}

}

public string LastName

{

get {return this.lastname;}

set {this.lastname = value;}

}

public string Age

{

get {return this.age;}

set {this.age = value;}

}

}

2. Interface Definition:

Code

/**////<summary >

Define the query interface.

</summary >

public interface Iqueryparams

{

/**////<summary >

Parameters

</summary >

Queryparams Parameters {get;}

}

3, Query page Inheritance Iqueryparams interface (querypage.aspx):

Querypage.aspx

Code

<form id= "Form1" runat= "Server" >

<div>

<asp:textbox id= "txtFirstName" runat= "Server" ></asp:TextBox>

<asp:textbox id= "Txtlastname" runat= "Server" ></asp:TextBox>

<asp:textbox id= "txtage" runat= "Server" ></asp:TextBox>

<asp:button id= "Btnenter" runat= "Server" text= "button"

onclick= "Btnenter_click"/></div>

</form>

QueryPage.aspx.cs

Code

Public partial class QueryPage:System.Web.UI.Page, Iqueryparams

{

Private Queryparams Queryparams;

Public Queryparams Parameters

{

Get

{

return queryparams;

}

}

public void Btnenter_click (object sender,

System.EventArgs e)

{

assigning values

Queryparams = new Queryparams ();

Queryparams.firstnname = This.txtFirstName.Text;

Queryparams.lastname = This.txtLastName.Text;

Queryparams.age = This.txtAge.Text;

Server.Transfer ("resultpage.aspx");

}

protected void Page_Load (object sender, EventArgs e)

{ }

}

4, receive page (resultpage.aspx):

ResultPage.aspx.cs

public partial class ResultPage:System.Web.UI.Page

{

protected void Page_Load (object sender, EventArgs e)

{

Queryparams queryparams = new Queryparams ();

Iqueryparams QueryInterface;

The page that implements the interface

if (Context.Handler is Iqueryparams)

{

QueryInterface = (iqueryparams) Context.Handler;

Queryparams = queryinterface.parameters;

}

Response.Write ("FirstName:");

Response.Write (Queryparams.firstname);

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

Response.Write (Queryparams.lastname);

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

Response.Write (Queryparams.age);

}

}

7. Other ways
Add a line to the HTML code for the receiving page:
<%@ Reference Page = "WebForm1.aspx"%>
WebForm1 fp= (WebForm1) Context.Handler;
This. Textbox1.text=fp.name; Name is the public variable for the first page

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.