abc
abc

Reputation: 215

Pass listbox items from page1 to page2 using sessioon

Can anyone show an example how listbox and session work. E.g to pass listbox items from page1 to page2. thx!

            if (!Page.IsPostBack)
            {
                lstFarger.Items.Add(blo);
                lstFarger.Items.Add(gron);
                lstFarger.Items.Add(brun);
                lstFarger.Items.Add(gul);
            }
        }

        protected void btnLaggTill_Click(object sender, EventArgs e)
        {
            if (!lstLagdaFarger.Items.Contains(lstFarger.SelectedItem) && lstLagdaFarger.Items.Count<3)
            {
                lstLagdaFarger.Items.Add(lstFarger.SelectedItem.Text);

            }
            else
            {
                lblFelMeddelande.Visible = true;
            }
            if (lstLagdaFarger.Items.Count == 3)
            {

                Response.Redirect("WebForm2.aspx");
            }

Upvotes: 0

Views: 32

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460228

Why do you need to pass ListItems, you can save the datasource in session. However, is it really necessary to use the Session, why can't you select the data again? You gain not much but a Session needs server memory and also could contain outdated data.

Having said that, if you really want to use the session to store the listitems...

In your button click handler:

if (lstLagdaFarger.Items.Count == 3)
{
    Session["LagdaFargerItems"] = lstLagdaFarger.Items;
    Response.Redirect("WebForm2.aspx");
}

in page 2:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack && Session["LagdaFargerItems"] != null)
    { 
        ListItemCollection lic = (ListItemCollection)Session["LagdaFargerItems"];
        foreach(ListItem li in lic)
            lstLagdaFarger.Items.Add(li); // your other ListBox on page2
    }
}

Upvotes: 1

Related Questions