简体   繁体   中英

How to retain values in my listbox between postbacks ASP.NET

I'm fairly new to asp.net, any help would be greatly appreciated. The program is really simple, I have two web pages, one contains a listbox and the other contains a textbox. The program is just meant to add items to the listbox from another page. My code on WebForm1 ,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestAppState
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)    
            {
                string field1 = (string)(Session["id"]);
                ListBox1.Items.Add(field1);        
            }
            else
            {
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebForm2.aspx");
        }
    }
}

Code on WebForm2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Caching;

namespace TestAppState
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Application["id"] = txtID.Text;
            Response.Redirect("WebForm1.aspx");
        }
    }
}

You're almost there. In your btn_AddClick event you need to find all of the ListBox items that have been selected and save their values to the session. You could do this in any number of formats, but probably a simple comma separated list would be fine.

The in your Page_Load event on WebForm2, you can read that session value and either directly data bind the text box with your session value or do whatever you need to do beforehand to manipulate the data.

In the code-behind for your second form, you could also snatch values directly from the first form by using the Control.FindControl method. Query strings could be yet another option. Not to knock other answers but sessions can be tricky some times. Although they are an easy solution, they can be lost depending on your application and in some cases, depending on browser security settings. This is mainly a concern using HTTPS though. Another reference for this might be Cross-Page Posting in ASP.NET Web Pages .

There are many different ways to achieve what you need to do. My approach would be to embed both pages into one form and manipulate the data with JavaScript or jQuery and use WebMethods to prevent from using postbacks. It would be a much smoother presentation to the user as well.

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