Release of several methods for passing parameters between Asp.net pages

Source: Internet
Author: User

 

Because some parameters need to be passed between two pages in the project, the following methods for passing parameters are summarized:

Method 1:
Pass through URL link address
Send. aspx:

Copy the Code as follows:
Protected void button#click (Object sender, eventargs E)
{
Request. Redirect ("default2.aspx? Username = honge ");
}

Receive. aspx:
Copy the Code as follows:
String username = request. querystring ["username"]; To obtain the parameter value.

Method 2:
Send. aspx
Copy the Code as follows:
<Form ID = "form1" runat = "server" Action = "receive. aspx" method = post>
<Div>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/>
<Asp: textbox id = "username" runat = "server"> </ASP: textbox>
</Div>
</Form>

Receive. aspx
Copy the Code as follows:
String username = ruquest. Form ["receive"];

Method 3:
Send. aspx:
Copy the Code as follows:
Protected void button#click (Object sender, eventargs E)
{
Session ["username"] = "honge ";
Request. Redirect ("default2.aspx ");
}

\ Receive. aspx:
Copy the Code as follows:
String username = session ["username"]; To obtain the parameter value.

Method 4:
Send. aspx:
Copy the Code as follows:
Protected void button#click (Object sender, eventargs E)
{
Application ["username"] = "honge ";
Request. Redirect ("default2.aspx ");

}

Receive. aspx:
Copy the Code as follows:
String username = application ["username"]; To obtain the parameter value.

Method 5:
Send. aspx:
Copy the Code as follows:
Public string name
{
Get {
Return "honge ";
}
}
Protected void button#click (Object sender, eventargs E)
{
Server. Transfer ("default2.aspx ");
}

Receive. aspx:
Copy the Code as follows:
Send d = context. handler as send;
If (D! = NULL)
{
Response. Write (D. Name); to obtain the parameter value.
}

In Asp.net 2.0, you can use the following method:
Copy the Code as follows:
Previouspage d = context. handler as previouspage;
If (D! = NULL)
{
Response. Write (D. Name); to obtain the parameter value.
}

It can also be used as follows:
Send. aspx:
Copy the Code as follows:
<Asp: button id = "btnsubmit" runat = "server" postbackurl = "~ /Reveive. aspx "text =" Submit "/>

Receive. aspx:
Copy the Code as follows:
<% @ Previouspagetype virtualpath = "~ /Default. aspx "%>
String name = previouspage. Name; to obtain the parameter value.

Supplement: In data binding controls such as datalist and repeater
<A href = "article. aspx? Id = <% # databinder. eval (container. dataitem, "article_id") %> "target =" _ blank "> // go to the link page and upload the parameter ID. The ID is the field in the database;

}

Receive. aspx:
Copy the Code as follows:
String username = application ["username"]; To obtain the parameter value.

Method 5:
Send. aspx:
Copy the Code as follows:
Public string name
{
Get {
Return "honge ";
}
}
Protected void button#click (Object sender, eventargs E)
{
Server. Transfer ("default2.aspx ");
}

Receive. aspx:
Copy the Code as follows:
Send d = context. handler as send;
If (D! = NULL)
{
Response. Write (D. Name); to obtain the parameter value.
}

In Asp.net 2.0, you can use the following method:
Copy the Code as follows:
Previouspage d = context. handler as previouspage;
If (D! = NULL)
{
Response. Write (D. Name); to obtain the parameter value.
}

It can also be used as follows:
Send. aspx:
Copy the Code as follows:
<Asp: button id = "btnsubmit" runat = "server" postbackurl = "~ /Reveive. aspx "text =" Submit "/>

Receive. aspx:
Copy the Code as follows:
<% @ Previouspagetype virtualpath = "~ /Default. aspx "%>
String name = previouspage. Name; to obtain the parameter value.

Supplement: In data binding controls such as datalist and repeater
<A href = "article. aspx? Id = <% # databinder. eval (container. dataitem, "article_id") %> "target =" _ blank "> // go to the link page and upload the parameter ID. The ID is the field in the database;

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.