C #: summarize several methods for passing values on the page

Source: Internet
Author: User
Tags set cookie

TIPS:

1. W7 comes with. NetFrameWork 3.5, And the compatibility mode is later compatible with lower versions;

2. Both WF and WPF are based on XAML, but they are used differently.

WF is a development Framework that embeds workflows in. NET Framework applications. It is mainly used to develop and create workflow applications. WF: http://msdn.microsoft.com/zh-cn/library/ms734696.aspx

WPF is a new graphic display system used for Windows platform to render the UI.. NET Framework.. NET Framework. WPF: http://msdn.microsoft.com/zh-cn/library/ms742119 (v = vs.100)



1. Use QueryString

Response. Redirect (url );

Request. QueryString [""];


2. Use Session Variables

Add necessary controls to the page to create buttons and links that can return the form. In the Click Event of the button or link button, add the control value to the session variable and use Response. redirect (or Server. the Transfer method redirects to another page to extract the value of the session on another page. When you determine that you do not need to use this session, you must explicitly clear it.

Session ["name"] = TextBox. Text;
Server. Transfer ("WebForm2.aspx ");

Label2.Text = Session ["name"]. ToString ();
Session. Remove ("name ");


3. Use Server. Transfer

Add the necessary controls on the page to create the Get attribute of the returned value. You can create the buttons and links of the return form. Click the buttons in the event handler to call Server. transfer the Transfer method to the specified page. In the second page, we can use Context. the Handler property is used to obtain the reference of the previous page instance object. With it, you can access the value of the control on the previous page.

Example 1:

Get
{
Return TextBox1.Text;
}

Private void button#click (object sender, System. EventArgs e)
{
Server. Transfer ("WebForm2.aspx ");
}

In WebForm2.aspx, add <% @ Reference Page = "~ /WebForm1.aspx "%> or <% @ PreviousPageType VirtualPath = "~ /WebForm1.aspx "%>
Add the following content to WebForm2.aspx. cs:
WebForm1 wf1;
Wf1 = (WebForm1) Context. Handler;
Label1.Text = wf1.Name;
Example 2:

 This can be said to be the method used for face object development. It uses Server. the Transfer method directs the process from the current page to another page. The new page uses the response stream of the previous page. Therefore, this method is completely facial and effective. The following code shows the method used when many parameters are required. If there are few parameters, it is unnecessary to use this method.
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:


///
/// Summary of QueryParams
/// <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3VtbWFyeT48YnI + CjxpbWcgc3JjPQ = "http://www.bkjia.com/uploads/allimg/140705/002Z34330-27.gif" align = "top" alt = "\"> 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:


///
/// Define the query interface.
///
Public interface IQueryParams
{
///
/// Parameters
///
QueryParams Parameters {get ;}
}


3. the query page inherits the IQueryParams interface (QueryPage. aspx ):
QueryPage. aspx




QueryPage. aspx. cs


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)
{
// Assign a value
QueryParams = new QueryParams ();
QueryParams. FirstnName = this.txt FirstName. Text;
QueryParams. Lastname = this.txt LastName. Text;
QueryParams. Age = this.txt Age. Text;
Server. Transfer ("ResultPage. aspx ");
}

Protected void Page_Load (object sender, EventArgs e)
{}
}

4. Receiving 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;
// Page for implementing this interface
If (Context. Handler is IQueryParams)
{
QueryInterface = (IQueryParams) Context. Handler;
QueryParams = queryInterface. Parameters;
}

Response. Write ("FirstName :");
Response. Write (queryParams. FirstName );
Response. Write ("
Lastname :");
Response. Write (queryParams. LastName );
Response. Write ("
Age :");
Response. Write (queryParams. Age );

}
}

4. Use the PostBackUrl attribute of some controls

Example: The Source Page is still WebForm1.aspx and the target page is WebForm2.aspx.

Some code in WebForm1.aspx:

Some code in WebForm2.aspx. cs:

Protected void Page_Load (objectSender, System. EventArgs e)

{

TextBoxtxtName;

Calendarcalendar1;

TxtName = (TextBox) PreviousPage. FindControl ("txtName ");

Calendar1 = (Calendar) PreviousPage. FindControl ("Calendar1 ");

Label. Text = "Hello," + txtName. Text + calendar1.SelectedDate. tow.datestring ();

}

This method has a problem: what if someone requested WebForm2.aspx before clicking the button, that is, before WebForm1.aspx is not processed? Therefore, you need to add a judgment before processing the code in WebForm2.aspx. Use the IsCrossPagePostBack attribute, which is similar to the IsPostBack attribute. It allows you to check whether the request comes from WebForm1.aspx:

Protected void Page_Load (objectSender, System. EventArgs e)

{

If (PreviousPage. IsCrossPagePostBack)

{

TextBox txtName;

Calendar calendar1;

TxtName = (TextBox) PreviousPage. FindControl ("txtName ");

Calendar1 = (Calendar) PreviousPage. FindControl ("Calendar1 ");

Label. Text = "Hello," + txtName. Text + calendar1.SelectedDate. tow.datestring ();

}

Else

{

Response. Redirect ("WebForm1.aspx ");

}

}


5.Use the @ PreviousPageType command

TypeName: Specifies the name of the derived class during the delivery.

VirtualPath: Set the address of the page to be sent during delivery.

WebForm1.aspx

Getmediareturnthis.txt Name;} // returns a control object.


<% @ PreviousPageTypeVirtualPath = "~ /Page1.aspx "%>,

Then you can reference the attributes defined in WebForm1.aspx.

In WebForm2.aspx. cs, You can reference the following format (assume that WebForm2.aspx has a Label with the ID of lblName ):

LblName. Text = "Hello" + PreviousPage. Name. Text +"
";


6. Use Cookie object variables

Like Session, it is for every user, but there is an essential difference: Cookie is stored on the client, and session is stored on the server. In addition, cookies must be used in combination with ASP. NET built-in object requests.

Set Cookie: HttpCookie cookie_name = new HttpCookie ("name ");
Cookie_name.Value = Label1.Text;
Reponse. AppendCookie (cookie_name );

Get Cookie:
String name = Request. Cookie ["name"]. Value. ToString ();

7. Use the Application object variable

The scope of the Application object is global, that is, it is valid for all users. The common methods are Lock and UnLock.

Application ["name"] = Label1.Text;
Server. Transfer ("B. aspx ");

String name;
Application. Lock ();
Name = Application ["name"]. ToString ();
Application. UnLock ();
}

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.