简体   繁体   中英

How do I pass variable between .aspx pages after clicking [OK] in a C# web form

I'm new to web forms.

1) My default web form is Default.aspx. It has a couple of combo boxes, and a Button control: all ASP.Net web controls.

2) In the Page_load(), I create a C# object "ScoringInfo ()":

protected void Page_Load(object sender, EventArgs e)
    {
        scoringInfo = new ScoringInfo();
        ...

3) ScoringInfo reads some info from a database into member variables, and uses the member variables to fill the combo boxes:

scoringInfo.GetOpenCrossByDate(dt, openCrossInfo);
    cbAvailableBowlers.Items.Clear ();
    foreach (OpenCrossInfoRec rec in openCrossInfo)
      string s =
        String.Format(
          "Lane {0:00}: {1}",
          rec.laneNo, rec.dateTime);
      cbAvailableBowlers.Items.Add(s);
      ...

4) Here are the member variables:

...
  protected ScoringInfo scoringInfo;
  protected List leagueInfo = new List();
  protected List openCrossInfo = new List();

5) When the user presses the button, I want to display a second .aspx page which processes the specific combo box item the user selected. Here is my "OnClick" event handler:

protected void bTest_Click1(object sender, EventArgs e)
        {
          int idx = cbAvailableBowlers.SelectedIndex;
          Session["openCrossLaneUniqueId"] = openCrossInfo[idx].laneUniqueId;
          ...// THIS FAILS:
             // "ARGUMENT OUT OF RANGE" exception;
             // "idx" is 0; openCrossInfo[] list is empty...

It doesn't work ... because member variable, "openCrossInfo[]" and combo box property SelectedIndex don't seem to be valid any longer when bTest_Click1 is executed!

How/where do I save the state of the UI for other, subsequent pages in the same session?

Here are five ways to pass data between web pages:

http://www.itorian.com/2012/07/5-ways-to-send-data-between-aspnet-pages.html

The member variables for the page (such as openCrossInfo) will not persist from request to request. The Page object for the .aspx is created again each time a new request comes in. So when the event for bTest_Click fires, it is working with a new copy of the Page object. Your openCrossInfo array has no values because the page object was just freshly created, even though you set it in an earlier request.

If you want save state you will have to use something else such as Session state.

The problem, as Jay Douglass pointed out, is that the member variable "openCrossInfo" from the original page isn't persisted to the new, "postback" page.

The solution was:

  1. create and initialize the objects in the original page ("if !IsPostBack"), save the initialized objects to the Session, then
  2. restore them from the Session for the subsequent page:


protected void Page_Load(object sender, EventArgs e)
{
    scoringInfo = new ScoringInfo();
    if (!IsPostBack)
    {
        // 1st time, use current date/time; create new data
        leagueInfo = new List<LeagueInfoRec>();
        openCrossInfo = new List<OpenCrossInfoRec>();
        laneUniqueIds = new List<string>();
        updateGui(DateTime.Now);
        Session["leagueInfo"] = leagueInfo;
        Session["openCrossInfo"] = openCrossInfo;
        Session["laneUniqueIds"] = laneUniqueIds;
    }
    else
    {
        // Subsequent callbacks: retrieve state
        leagueInfo = (List<LeagueInfoRec>)Session["leagueInfo"];
        openCrossInfo = (List<OpenCrossInfoRec>)Session["openCrossInfo"];
        laneUniqueIds = (List<string>)Session["laneUniqueIds"];
    }
}

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