Results 1 to 7 of 7

Thread: Working with ViewState

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Working with ViewState

    I am saving uploadPaths into the ViewState by doing this
    VB Code:
    1. Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    2.     i = 1 'declared as global variable
    3.     uploadCount = 1 'declared as global variable
    4.  
    5.     ViewState("UploadPath" & i) = attachmentFile.PostedFile.FileName
    6.  
    7.     'rest of code
    8.  
    9.     uploadCount = +1
    10.     i += 1
    11. End Sub

    When I view the data in ViewState I can see the first uploaded value, but it doesn't save any additional uploads into ViewState.

    And when I try to read the values from ViewState
    VB Code:
    1. For i = 1 To uploadCount
    2.     Response.Write("UploadPath: " & ViewState("UploadPath" & i) & "<br>")
    3. Next
    It isn't returning any values....Why is this...Am I missing something???
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    If your variable i is not also being saved to viewstate then it's probably getting reinitialized on every postback, probably also why you only see the first one. You can set up a property to maintain that upload count in viewstate like this:
    VB Code:
    1. <script language="vb" runat="server">
    2. Public Property UploadCount() As Integer
    3.     Get
    4.         If Not ViewState("UploadCount") Is Nothing Then
    5.             Return System.Convert.ToInt32(ViewState("UploadCount"))
    6.         Else
    7.             Return 0
    8.         End If
    9.     End Get
    10.     Set(ByVal Value As Integer)
    11.         ViewState("UploadCount") = Value
    12.     End Set
    13. End Property
    14. Public Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    15.     Dim viewStateKey As String = "UploadPath" & UploadCount.ToString()
    16.     ViewState(viewStateKey) = "SomePath" & UploadCount.ToString()
    17.     UploadCount += 1
    18. End Sub
    19. Public Sub btnShowUploads_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    20.     For i As Integer = 0 To UploadCount - 1
    21.         Response.Write(ViewState("UploadPath" & i.ToString()) & "<br/>")
    22.     Next
    23. End Sub
    24. </script>
    25. <html>
    26.     <body>
    27.         <form runat="server">
    28.             <asp:Button ID="btnUpload" Runat="server"
    29.                 Text="Upload" OnClick="btnUpload_Click"/>
    30.             <asp:Button ID="btnShowUploads" Runat="server"
    31.             Text="Show Uploads" OnClick="btnShowUploads_Click"/>
    32.         </form>
    33.     </body>
    34. </html>

  3. #3

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    SCHWEET!...I'll give it a try...thanks
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Thanks for the help...The code you provided was perfect.

    Now my application can add the path of the upload to viewstate and if the user wants to remove the upload, It will also remove the value from the ViewState by using the StateBag.Remove method.

    Thanks!
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    My love and hate (mostly hate, mostly at myself and/or anyone in yelling or spitting distance) for ASP.NET.

    I work at a small company (4 people), trying to take over our industry. We work hard, our pay not what it should be, but all in the promise for a better tomorrow.

    We were developing this beautiful windows application. It had more features and abilities than any of our competitors, including competitor Z with 22 programmers. It shined, it really did. It made me wonder what competitor Z's programmers did all day. I imagined them talking about the Simpson's or last night's football game for the first three hours, typing some code, and then perhaps taking an hour lunch, followed by another hour of coding, and then they would all just take the rest of the week off.

    Everything was grand, we were on track to dropping the bomb on our competitors within weeks. Then something happened. And when that something happened, and even as it was happening, I had wished it had happened long before it actually happened or happening.

    Our biggest prospective client calls our boss, and states 'No sale. We need a thin client'.

    I can't begin to accurately describe sitting in a chair, absolutely numb in time as reality hits you from as many angles as the universe can spare. It shocked enough that I can still easily envision that moment an infinite number of times, if that tells you anything. And I'm not even the project manager.

    In classic style that only a company of 4 or 5 people (5 if you include the '1 part graphics designer,2 parts tech support' guy) can do, we were within the hour, running test applications galore for the next 2 weeks.

    It's now almost 2 months later, and we THINK we have our custom server controls nailed. I spent more time futzing with viewstate and postbacks and IPOSTBACKDATAHANDLERS than actually breathing. And the ASP.Net books, 5 in total, all clearly detailing how to create and implement custom controls which unfortunately are too simplistic to relate to real-world implementation. What the #$#@ good is an example if you would never use it anyway?

    The first control I had to make ended up being custom composite control that included two textboxes, three validators, along with rendering 5 discrete javascript blocks based on the former textboxes' properties. And that was just a date/time picker.

    Next up was a custom composite control of three or more textboxes (again depending on what mode the control was in), that would ultimately tell the page if our custom controls should render in edit mode or read only.

    My 2 month introduction to ASP.NET was fast and hard, and it still amazes me, everytime I go for the F5 key, I hesitate with a cringe.

    It all boils down to this: Writing controls for the web has to be one of the most frustrating experiences in programming. -STOP
    Last edited by nemaroller; Jan 10th, 2004 at 01:55 AM.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Bummer... Sorry to hear about the ordeal. So your story is like a cliffhanger man! did you guys get that client back or no? I assume since you spent so much time on the asp.net stuff that there was a pretty good chance of the sale.

    My learning curve was similar; beyond simple controls viewstate drove me nuts - when i could use it, when i could change it and have my changes stick, what i could save to it, etc. In hind sight(as always) if i had first learned the basics of the page life cycle and a few other "minor" details, i think my learning curve would have been a little flatter. But for the most part those "Hello World" type examples sucked, the asp.net newsgroups and some choice sites were far better(and cheaper) than all of the asp.net books i got(i got the Wrox and the Unleashed and a few others).

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Yea we got the sale back.... called them the next day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width