C implementation.net page between the value pass reference method summary

  • 2020-11-30 08:30:39
  • OfStack

This article summarizes the example of C# implementation.net page between value and reference method. Share to everybody for everybody reference. Specific implementation methods are analyzed as follows:

1. QueryString spread value

In general, QueryString is a very simple way of passing values that can be displayed in the browser's address bar. You can use this method if you are passing one or more values that are less secure or simple in structure. But if you're passing arrays or objects, you can't use this method. As shown in the following example:

private void Button1_Click(object sender, System.EventArgs e)
{
     string s_url;
     s_url = "b.aspx?name=" + Label1.Text;
     Response.Redirect(s_url);
}

b. C# in aspx:
private void Page_Load(object sender, EventArgs e)
{
     Label2.Text = Request.QueryString["name"];
}

query passvalue also classifies post,get format as

//post request  
string name = Request["name"].toString();
string name =Request.Form.Get("name").toString();
//get request
string name = Request.QueryString["name"].toString();

But I found that it works with post and get values

string name = Request["name"].toString();

The differences between get and post methods in form submission are summarized as follows:

1. get gets data from the server and post sends data to the server.
2. get is to add the parameter data queue to URL indicated by the ACTION attribute of the form submission. The value corresponds to each field 11 in the form, which can be seen in URL. post USES the HTTP post mechanism to place each field in the form and its contents in HTML HEADER 1 and send it to the URL address indicated by the ACTION attribute. This process is invisible to the user.
3. For get mode, the server side USES ES52en.QueryString to get the value of the variable, and for post mode, the server USES ES55en.Form to get the submitted data.
4. The amount of data transmitted by get is small and cannot be greater than 2KB. post transmits a large amount of data and is generally unrestricted by default. However, theoretically, the maximum number of IIS4 is 80KB and IIS5 is 100KB.
5. get has very low security, while post has high security.

2. Use Application object variables

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

a. C# code for aspx

private void Button1_Click(object sender, System.EventArgs e)
{
     Application["name"] = Label1.Text;
     Server.Transfer("b.aspx");
}

b.aspx C# code
private void Page_Load(object sender, EventArgs e)
{
     string name;
     Application.Lock();
     name = Application["name"].ToString();
     Application.UnLock();
}

3. Use Session variable

This is probably the most common usage you'll see, as it operates like Application and ACTS on individual users, so too much storage can cause the server to run out of memory resources.

C# code for a.aspx

private void Button1_Click(object sender, System.EventArgs e)
{
     Session["name"] = Label.Text;
}

b. C# code in aspx
private void Page_Load(object sender, EventArgs e)
{
     string name;
     name = Session["name"].ToString();
}

4. Use Cookie object variables

This is also a common approach, similar to Session1, which is for every user, but with the essential difference that Cookie is on the client side and session is on the server side. And Cookie should be used with ASP. NET built-in object Request to use.

a.aspx's C# code

private void Button1_Click(object sender, System.EventArgs e)
{
     HttpCookie cookie_name = new HttpCookie("name");
     cookie_name.Value = Label1.Text;
     Reponse.AppendCookie(cookie_name);
     Server.Transfer("b.aspx");
}

b. C# code in aspx
private void Page_Load(object sender, EventArgs e)
{
     string name;
     name = Request.Cookie["name"].Value.ToString();
}

5. Server. Transfer method is used

Server.Transfer directs the flow from the current page to another page. The new page USES the response flow from the previous page, so this method is completely object-like, concise and effective.

a.aspx's C# code

private void Page_Load(object sender, EventArgs e)
{
     Label2.Text = Request.QueryString["name"];
}
0
b. C# code in aspx
private void Page_Load(object sender, EventArgs e)
{
     a newWeb;   // The instance a The form
     newWeb = (source)Context.Handler;
     string name;
     name = newWeb.Name;
}

These are summarized, but the most used is QueryString.

Hopefully this article has helped you with your C# programming.


Related articles: