SoFunction
Updated on 2025-03-06

C# implements summary of methods for passing values ​​and parameters between .net pages

This article summarizes the method of passing values ​​and parameters between C# pages. Share it for your reference. The specific implementation method is analyzed as follows:

1. QueryString value transmission

Generally speaking, QueryString is a very simple way to pass values. It can display the transmitted values ​​in the browser's address bar. This method can be used if you pass one or more values ​​with low security requirements or simple structure. However, this method cannot be used when passing arrays or objects. As shown in the following example:

Copy the codeThe code is as follows:
private void Button1_Click(object sender, e)
{
     string s_url;
     s_url = "?name=" + ;
     (s_url);
}

C# code:
Copy the codeThe code is as follows:
private void Page_Load(object sender, EventArgs e)
{
     = ["name"];
}

query value is also classified as post, get format

Copy the codeThe code is as follows:
//Post request
string name = Request["name"].toString();
string name =("name").toString();
//get request
string name = ["name"].toString();

But I found that whether it is post or get value transmission is available

Copy the codeThe code is as follows:
string name = Request["name"].toString();

The difference between get and post methods in form submission is summarized as follows:

1. Get is to get data from the server, and post is to transfer data to the server.
2. get adds the parameter data queue to the URL referred to by the ACTION attribute of the submitted form. The value corresponds to each field in the form one by one, and can be seen in the URL. Post is to pass the HTTP post mechanism to place each field in the form and its content in the HTML HEADER to the URL referred to by the ACTION attribute. Users cannot see this process.
3. For the get method, the server uses the value of the variable to get, and for the post method, the server uses the submitted data.
4. The amount of data transmitted by get is small and cannot be greater than 2KB. The amount of data transmitted by post is large and is generally defaulted to be unrestricted. But theoretically, the maximum amount in IIS4 is 80KB and 100KB in IIS5.
5. Get security is very low and post security is high.

2. Use Application object variables

The scope of the Application object is the entire global, which means it is valid for all users. The commonly used methods are Lock and UnLock.

C# code

Copy the codeThe code is as follows:
private void Button1_Click(object sender, e)
{
     Application["name"] = ;
     ("");
}

C# code
Copy the codeThe code is as follows:
private void Page_Load(object sender, EventArgs e)
{
     string name;
     ();
     name = Application["name"].ToString();
     ();
}

3. Use Session variables

I believe this is definitely the most common usage in everyone's use. Its operation is similar to Application and acts on the user's personal, so excessive storage will lead to the exhaustion of server memory resources.

C# code

Copy the codeThe code is as follows:
private void Button1_Click(object sender, e)
{
     Session["name"] = ;
}

C# code
Copy the codeThe code is as follows:
private void Page_Load(object sender, EventArgs e)
{
     string name;
     name = Session["name"].ToString();
}

4. Use cookie object variables

This is also a method that everyone often uses. Like Session, it is for every user, but there is an essential difference, that is, cookies are stored on the client side, while sessions are stored on the server side. Moreover, the use of cookies should be used in conjunction with the built-in object Request.

C# code

Copy the codeThe code is as follows:
private void Button1_Click(object sender, e)
{
     HttpCookie cookie_name = new HttpCookie("name");
     cookie_name.Value = ;
     (cookie_name);
     ("");
}

C# code
Copy the codeThe code is as follows:
private void Page_Load(object sender, EventArgs e)
{
     string name;
     name = ["name"].();
}

5. How to use

This can be said to be the method used in the development of facial objects. Its usage guides the process from the current page to another page, and the new page uses the response flow of the previous page, so this method is completely facial objects, concise and effective.

C# code

Copy the codeThe code is as follows:
public string Name
{
     get{ return ;}
}
private void Button1_Click(object sender, e)
{
     ("");
}

C# code
Copy the codeThe code is as follows:
private void Page_Load(object sender, EventArgs e)
{
a newWeb;   //Instance a form
     newWeb = (source);
     string name;
     name = ;
}

I have summarized these items, but the one I use most is QueryString.

I hope this article will be helpful to everyone's C# programming.