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. ViewState problem...

ViewState problem...

Scheduled Pinned Locked Moved Web Development
helpquestion
4 Posts 2 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.
  • B Offline
    B Offline
    brian55
    wrote on last edited by
    #1

    In one form I save a variable in a view state like so: ViewState["Color"] = "red"; I then call Response.Redirect(nextPage.aspx) In the page load function of nextPage.aspx I try and read the variable using this: string sColor=(string) ViewState["Color"]; but there is nothing there! What am I doing wrong? thanks Brian

    M 1 Reply Last reply
    0
    • B brian55

      In one form I save a variable in a view state like so: ViewState["Color"] = "red"; I then call Response.Redirect(nextPage.aspx) In the page load function of nextPage.aspx I try and read the variable using this: string sColor=(string) ViewState["Color"]; but there is nothing there! What am I doing wrong? thanks Brian

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi Brian. ViewState is maintained for one page and its postbacks. When you redirect to another page, a new ViewState is generated. In your context, you may want to set a cookie rather than use ViewState, or use a Session variable: Session["Color"] = "red";

      B 1 Reply Last reply
      0
      • M Mike Ellison

        Hi Brian. ViewState is maintained for one page and its postbacks. When you redirect to another page, a new ViewState is generated. In your context, you may want to set a cookie rather than use ViewState, or use a Session variable: Session["Color"] = "red";

        B Offline
        B Offline
        brian55
        wrote on last edited by
        #3

        Thanks Mike. I did just that and things are working. Perhaps you can answer this question for me If I defne an aspx page with html controls, can I use code behined file to handle the code for those controls? This would mean that the controls are handled client side... Brian

        M 1 Reply Last reply
        0
        • B brian55

          Thanks Mike. I did just that and things are working. Perhaps you can answer this question for me If I defne an aspx page with html controls, can I use code behined file to handle the code for those controls? This would mean that the controls are handled client side... Brian

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          Hi Brian. The code-behind file (and the .aspx file for that matter) is compiled and executed server-side only. You can declare standard <html> controls with the runat="server" attribute to access them server-side. Here's a simple example:

          <%@ Page Language="C#" %>

          <script runat="server">

          void Page_Load(object o, EventArgs e)
          {
          myHeading.InnerHtml = "This is the Heading";
          myText.Value = "Default Value";
          }

          </script>

          <html>
          <head>
          <title></title>
          </head>

          <body>
          <form runat="server">

            <h3 id="myHeading" runat="server" />
            
            Here is a standard HTML text box:
            <input type="text" id="myText" runat="server" />
          
          </form>
          

          </body>

          </html>

          If you want to manipulate html controls client-side, you'll need to look at javascript for that.

          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