Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with viewstate for listitems

Problem with viewstate for listitems

Scheduled Pinned Locked Moved ASP.NET
helpdatabasecomquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eyeseetee
    wrote on last edited by
    #1

    Ive got an Onclick event which has the following code in: ViewState["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=items"); on the pageload it should then display the values chosen from listbox if the page has posted back. However when I click the Button it shows an Object Reference Error for the following line: ViewState["items"] = lb1.SelectedItem.Value; why is this? thanks

    Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

    T S 2 Replies Last reply
    0
    • E eyeseetee

      Ive got an Onclick event which has the following code in: ViewState["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=items"); on the pageload it should then display the values chosen from listbox if the page has posted back. However when I click the Button it shows an Object Reference Error for the following line: ViewState["items"] = lb1.SelectedItem.Value; why is this? thanks

      Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

      T Offline
      T Offline
      thomasa
      wrote on last edited by
      #2

      Seems to me that there there are no selected items when the button is click'ed.

      if(lb1.SelectedItem != null)
      {
      ViewState["items"] = lb1.SelectedItem.Value;
      }

      If the listbox allso has multiselect = true, you will allso get a problem. if so you should use

      string strValues = "";
      foreach(ListItem li in lb1.Items)
      {
      if(li.Selected)
      {
      strValuse+=li.Value;
      }
      }

      Hope it helps Thomas

      modified on Monday, October 6, 2008 10:31 AM

      E 2 Replies Last reply
      0
      • T thomasa

        Seems to me that there there are no selected items when the button is click'ed.

        if(lb1.SelectedItem != null)
        {
        ViewState["items"] = lb1.SelectedItem.Value;
        }

        If the listbox allso has multiselect = true, you will allso get a problem. if so you should use

        string strValues = "";
        foreach(ListItem li in lb1.Items)
        {
        if(li.Selected)
        {
        strValuse+=li.Value;
        }
        }

        Hope it helps Thomas

        modified on Monday, October 6, 2008 10:31 AM

        E Offline
        E Offline
        eyeseetee
        wrote on last edited by
        #3

        Thanks for the post but it doesnt work Ive got string strValues = ""; foreach (ListItem li in listbox1.Items) { if (li.Selected) { strValues += li.Value; } } Session["items"] = strValues; in the onclick event and then on pageload I have: lbitems.Text = "Items selected: " + Session["items"].ToString(); The label is shown as blank. I really dont understand why this is happening, very weird! Any more help would be great thanks Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

        T 1 Reply Last reply
        0
        • E eyeseetee

          Thanks for the post but it doesnt work Ive got string strValues = ""; foreach (ListItem li in listbox1.Items) { if (li.Selected) { strValues += li.Value; } } Session["items"] = strValues; in the onclick event and then on pageload I have: lbitems.Text = "Items selected: " + Session["items"].ToString(); The label is shown as blank. I really dont understand why this is happening, very weird! Any more help would be great thanks Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

          T Offline
          T Offline
          thomasa
          wrote on last edited by
          #4

          Session is not updated at the time you go to the next page so you should us ViewState or HttpContext.Current.Items["somevalues"] If you have multiselect, you should splitt the valuse by somthing, so you can retrive them Something lik:

          if(listbox1.SelectedItem != null){
          string strValues = "";
          for (int i = 0; i < listbox1.Items.Count;i++)
          {
          if(listbox1.Items[i].Selected){
          strValues += listbox1.Items[i].Value + ",";
          }
          strValues = strValues .Remove(strValues.Lenght - 1, 1); // To remove the last seperator
          }

          HttpContext.Current.Items["theItemsValues"] = strValues;
          }

          on the pageload of the new page:

          if(HttpContext.Current.Items.Contains("theItemsValues")) {
          lbitems.Text= "Items selected: " +HttpContext.Current.Items["theItemsValues"].ToString();
          }

          If you then actually needs the items values:

          string[] strListOfValues = HttpContext.Current.Items["theItemsValues"].ToString().Splitt(',');

          modified on Tuesday, October 7, 2008 5:45 AM

          E 1 Reply Last reply
          0
          • T thomasa

            Seems to me that there there are no selected items when the button is click'ed.

            if(lb1.SelectedItem != null)
            {
            ViewState["items"] = lb1.SelectedItem.Value;
            }

            If the listbox allso has multiselect = true, you will allso get a problem. if so you should use

            string strValues = "";
            foreach(ListItem li in lb1.Items)
            {
            if(li.Selected)
            {
            strValuse+=li.Value;
            }
            }

            Hope it helps Thomas

            modified on Monday, October 6, 2008 10:31 AM

            E Offline
            E Offline
            eyeseetee
            wrote on last edited by
            #5

            Still the problem persists. This is one of the strangest I have ever seen. Cant understand it.

            Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

            S 1 Reply Last reply
            0
            • T thomasa

              Session is not updated at the time you go to the next page so you should us ViewState or HttpContext.Current.Items["somevalues"] If you have multiselect, you should splitt the valuse by somthing, so you can retrive them Something lik:

              if(listbox1.SelectedItem != null){
              string strValues = "";
              for (int i = 0; i < listbox1.Items.Count;i++)
              {
              if(listbox1.Items[i].Selected){
              strValues += listbox1.Items[i].Value + ",";
              }
              strValues = strValues .Remove(strValues.Lenght - 1, 1); // To remove the last seperator
              }

              HttpContext.Current.Items["theItemsValues"] = strValues;
              }

              on the pageload of the new page:

              if(HttpContext.Current.Items.Contains("theItemsValues")) {
              lbitems.Text= "Items selected: " +HttpContext.Current.Items["theItemsValues"].ToString();
              }

              If you then actually needs the items values:

              string[] strListOfValues = HttpContext.Current.Items["theItemsValues"].ToString().Splitt(',');

              modified on Tuesday, October 7, 2008 5:45 AM

              E Offline
              E Offline
              eyeseetee
              wrote on last edited by
              #6

              Thanks, I will try that tomorrow and will get back to you, thnaks for the help!

              Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

              1 Reply Last reply
              0
              • E eyeseetee

                Still the problem persists. This is one of the strangest I have ever seen. Cant understand it.

                Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                S Offline
                S Offline
                Sandeep Akhare
                wrote on last edited by
                #7

                Are you showing List item with its id and description ? Check what is value and what is text for the list item Try to debug the application whats happening there Can you tell me what you have to do ?

                Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog

                1 Reply Last reply
                0
                • E eyeseetee

                  Ive got an Onclick event which has the following code in: ViewState["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=items"); on the pageload it should then display the values chosen from listbox if the page has posted back. However when I click the Button it shows an Object Reference Error for the following line: ViewState["items"] = lb1.SelectedItem.Value; why is this? thanks

                  Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                  S Offline
                  S Offline
                  Sneha Bisht
                  wrote on last edited by
                  #8

                  Session["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=session["items"].Tostring()); use Session state instread of Viewstate

                  E 1 Reply Last reply
                  0
                  • S Sneha Bisht

                    Session["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=session["items"].Tostring()); use Session state instread of Viewstate

                    E Offline
                    E Offline
                    eyeseetee
                    wrote on last edited by
                    #9

                    Right, below is my code. It is still not working, I thought a new day would bring new answers but no. PageLoad: lbitems.Text = "Items selected: " + Session["items"]; Button(This posts back to the same page): if (listbox1.SelectedItem != null) { string strValues = ""; for (int i = 0; i < listbox1.Items.Count - 1; i++) { if (listbox1.Items[i].Selected) { strValues += listbox1.Items[i].Value + ","; } } strValues += listbox1.Items[listbox1.Items.Count - 1].Value; Session["items"] = strValues; } Response.Redirect("~/main/Page.aspx?sql=items"); Why is this not working? It doesnt display any selected values. thanks Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                    T 1 Reply Last reply
                    0
                    • E eyeseetee

                      Right, below is my code. It is still not working, I thought a new day would bring new answers but no. PageLoad: lbitems.Text = "Items selected: " + Session["items"]; Button(This posts back to the same page): if (listbox1.SelectedItem != null) { string strValues = ""; for (int i = 0; i < listbox1.Items.Count - 1; i++) { if (listbox1.Items[i].Selected) { strValues += listbox1.Items[i].Value + ","; } } strValues += listbox1.Items[listbox1.Items.Count - 1].Value; Session["items"] = strValues; } Response.Redirect("~/main/Page.aspx?sql=items"); Why is this not working? It doesnt display any selected values. thanks Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"

                      T Offline
                      T Offline
                      thomasa
                      wrote on last edited by
                      #10

                      The code strValues += listbox1.Items[listbox1.Items.Count - 1].Value; is wrong becauce you will allways add the last item, no mather if it is selected. I would recomand the modefide answer I gave you. Do you redirect to the same page?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups