Home >Backend Development >C#.Net Tutorial >Methods and techniques for parsing values ​​passed in ASP.NET pages

Methods and techniques for parsing values ​​passed in ASP.NET pages

怪我咯
怪我咯Original
2017-04-01 10:30:041377browse

1. Use QueryStringVariable

QueryString is a very simple value-passing method. It can display the transferred value in the browser. In the address bar. This method can be used when passing one or more values ​​with low security requirements or simple structure. However, this method cannot be used when passing arrays or objects. Here is an example:
a.aspx’s C# code

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

b.aspx’s C# code

private void Page_Load(object sender, EventArgs e)
{
    Label2.Text = Request.QueryString["name"];
}

2. Using the Application object variable
The scope of the Application object is the entire Global, that is to say, it is valid for all users. The commonly used methods are Lock and UnLock. C# code in
a.aspx

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

C# code in b.aspx. #

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

3. Use

Session

Variables Presumably this is the most common usage among everyone. Its operation is similar to Application and affects the individual user. Therefore, excessive storage will cause Exhaustion of server memory resources. C# code in a.aspx

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

b.aspx

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

4. Using
Cookie

Object variable

This is also a commonly used method. Like Session, it is for each user, but there is an essential difference, that is, Cookie is stored on the client, while Session is stored on the client. Server-side. And the use of cookies must be used in conjunction with the ASP.NET built-in object Request in the 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.aspx. C# code

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

5. Use the Server.Transfer method

This can be said to be the method used in object-oriented development. It uses the Server.Transfer method to guide the process from the current page to another In a page, the new page uses the response stream of the previous page, so this method is completely object-oriented, simple and effective.

a.aspx C# code

public string Name
{
    get{ return Label1.Text;}
}
private void Button1_Click(object sender, System.EventArgs e)
{
    Server.Transfer("b.aspx");
}

b.aspx C# code

private void Page_Load(object sender, EventArgs e)
{
    a newWeb;   //实例a窗体
    newWeb = (source)Context.Handler;
    string name;
    name = newWeb.Name;
}


-->

The above is the detailed content of Methods and techniques for parsing values ​​passed in ASP.NET pages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn