简体   繁体   中英

how to store post variable so i can call the variable anytime in ASP.NET

i am developing a web for my final project,and im new to ASP.NET and this forum. thx to all who help me.

the question is... example i have 2 pages.

page1.aspx.cs (this page for receive variable from mikrokontroler via network module) example mikrokontroler send a variable "status" = 1

    protected void Page_Load(object sender, EventArgs e)
    {
        NameValueCollection POST = Request.Form;
        int STATUS;
        int responcode;

        try
        {
            A = int.Parse(POST["status"]);
        }
        catch (Exception)
        {
            status = 0;            
        }

        if (A == 1)
        {
            responcode = 200;   

            //when A = 1, i want to store A value to (buffer on something <-- this what i want to ask)).
            so i can call the value anytime in page2.               
        }
        else                
        {
            responcode = 400;              
        }

        Response.StatusCode = responcode;
    }
}

}

page2.aspx (in page 2 there is button and textbox)

protected void Button3_Click(object sender, EventArgs e)
    {
      /*when this button click,
     i want to show A value from page1
      */
    }

You can use Session

    NameValueCollection POST = Request.Form;
    int STATUS;
    int responcode;
    try
    {

        A = int.Parse(POST["status"]);

    }

    catch (Exception)
    {

        status = 0;

    }


    if (A == 1)
    {
        responcode = 200;   

        //when A = 1, i want to store A value to (buffer on something <-- this what i want to ask)).

        Session["Avalie"] = A;
        so i can call the value anytime in page2.


    }

    else

    {
        responcode = 400;              
    }

    Response.StatusCode = responcode;
}

}

and then on page 2

protected void Button3_Click(object sender, EventArgs e)
{
  /*when this button click,
 i want to show A value from page1
  */

 if(!String.IsNullOrEmpty( Session["Avalie"] ))
 int Aval = int.Parse(Session["Avalie"]);

}

You have a lot of options to store the variable value:

  1. session state: Session["status"]= A
  2. application state: Application["status"] = A
  3. asp net cache: using Cache.Add()
  4. database: here i would store also the timestamps, to trace the historic status of the controller.
  5. local XML file.

It all depends on the scope of the stored data: session data is local to the current user/session and will expire after a predefined timeout(def: 20mins), application will be global to all your users/sessions and will expire when you will restart the application (by iis, iisreset, recompiling...), cache is global and will expire based on the parameters of invocation, the database and xml are global and will maintain state.

In your case i would use database or application store, because the microcontroller and user live in different sessions and the app cache is not a suitable messaging mechanism while Xml introduces some problems on his own (eg: filesystem permissions, data replication...).

write:

Application["status"] = A;

read:

int A = 0;
bool result = int.TryParse(Application["status"],out A);

BTW: to parse the integer you can skip the try/catch part doing this:

int A = 0;
bool result = int.TryParse(POST["status"],out A);

in this case if unable to parse A will be equal to 0;

One option is to assign the value to a static variable in the first page.
Refer to Static Classes and Static Class Members (C# Programming Guide)

Another option is to use state variables as session state variables or application variables.
Refer to ASP.NET Session State Overview and ASP.NET Application State Overview

使用crosspagepostback将值从一个页面传递到另一个页面(在asp.net 2.0中引入)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM