0

Ok so I have created a page where the user enters their details then they click a button which produces their results in a ListBox. However what I want to do is when the user clicks the button it produces the results on another page in a Listbox. I have been able to get the session to work with a textbox but not a listbox.

Code:

page1:

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

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        List<string> myList;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                myList = new List<string>(); // first time it loads is not a postBack
                Session["listSession"] = myList;  // stick the list in a session variable
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Clear();
            myList = (List<string>)Session["listSession"]; // gets the list out of session
            myList.Add(TextBox1.Text + " " + TextBox2.Text + " " + TextBox3.Text);

            foreach (var inputString in myList)
            {
                ListBox1.Items.Add(inputString + "");
            }

        }
    }
}

Any help would be appriciated:

3
Contributors
5
Replies
6
Views
4 Years
Discussion Span
Last Post by JorgeM
0

this looks like everything is in one page? Where is your code for the second page? How are you trying to load the ListBox on the other page?

0

For my First page:

protected void Button1_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Clear();
            myList = (List<string>)Session["listSession"]; // gets the list out of session
            myList.Add(TextBox1.Text + " " + TextBox2.Text + " " + TextBox3.Text);

            foreach (var inputString in myList)
            {
                ListBox1.Items.Add(inputString + "");




            }

            Session["x"] = ListBox1.Items;
            Response.Redirect("WebForm2.aspx");

        }

Second page:

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

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            Label1.Text = Session["x"].ToString();

        }
    }
}

When it executes Label1 has the following error input:System.Web.UI.WebControls.ListItemCollection;

0

I believe that your problem is the session variable is not storing the value of a simple string. The label1.text property can not be assigned the value of that session variable. Instead of using a label control, why aren't you using a listbox on the second page.

0

Glad to hear that. You may consider posting what you did to fix your issue so that others that have a similar problem can benefit from this knowledge and experience.

Also, marking the thread as solved will also help people find solutions to their related issues.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.