I am saving uploadPaths into the ViewState by doing this
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
i = 1 'declared as global variable
uploadCount = 1 'declared as global variable
ViewState("UploadPath" & i) = attachmentFile.PostedFile.FileName
'rest of code
uploadCount = +1
i += 1
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
For i = 1 To uploadCount
Response.Write("UploadPath: " & ViewState("UploadPath" & i) & "<br>")
Next
It isn't returning any values...Why is this...Am I missing something?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:<script language="vb" runat="server">
Public Property UploadCount() As Integer
Get
If Not ViewState("UploadCount") Is Nothing Then
Return System.Convert.ToInt32(ViewState("UploadCount"))
Else
Return 0
End If
End Get
Set(ByVal Value As Integer)
ViewState("UploadCount") = Value
End Set
End Property
Public Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim viewStateKey As String = "UploadPath" & UploadCount.ToString()
ViewState(viewStateKey) = "SomePath" & UploadCount.ToString()
UploadCount += 1
End Sub
Public Sub btnShowUploads_Click(ByVal sender As Object, ByVal e As System.EventArgs)
For i As Integer = 0 To UploadCount - 1
Response.Write(ViewState("UploadPath" & i.ToString()) & "<br/>")
Next
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Button ID="btnUpload" Runat="server"
Text="Upload" OnClick="btnUpload_Click"/>
<asp:Button ID="btnShowUploads" Runat="server"
Text="Show Uploads" OnClick="btnShowUploads_Click"/>
</form>
</body>
</html>
SCHWEET!...I'll give it a try...thanks
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!
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
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).
Yea we got the sale back... called them the next day.
Thursday, March 22, 2012
Working with ViewState
Labels:
asp,
btnupload_click,
byval,
eventargs,
net,
object,
saving,
sub,
system,
thisprivate,
uploadpaths,
viewstate,
working
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment