|
-
May 14th, 2008, 10:58 AM
#1
Thread Starter
Addicted Member
Using ViewState
I'm trying to learn to use ViewState but I guess I'm a little confuse. On a logon page, I have the following:
Code:
Dim vsViewState As classData.vsInfo (vsInfo is a serializable struture)
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Me.ViewState.Add("ViewStateData", vsViewState)
End Sub
When you user enter their username/password and choose a session that they logon, I define the following:
Code:
Protected Sub btnLogon_Click(ByVal sender As Object, ByVal e As System.EventArgs)
vsViewState.Username = getUserName()
vsViewState.Password = getPassword()
vsViewState.GroupID = getGroupID()
End Sub
After the user logon, I want to display the username/pass & the group that they login on the next page:
Code:
Dim vsViewState As classData.vsInfo (vsInfo is a serializable struture)
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Me.ViewState.Add("ViewStateData", vsViewState)
End Sub
Code:
Public ReadOnly Property ViewStateInfo() As classData.vsInfo
Get
Dim vsValues As classData.vsInfo
vsValues = CType(ViewState("ViewStateData"), classData.vsInfo)
Return vsInfo
End Get
End Property
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim groupID As String = ViewStateInfo.GroupID
Dim username As String = ViewStateInfo.Username
End Sub
My groupID & username is nothing...do you know why?
-
May 14th, 2008, 11:17 AM
#2
Re: Using ViewState
If you want to store custom objects in the ViewState or ControlState you have to override the appropriate methods on the page.
So for ViewState you would need to override LoadViewState and SaveViewState. Make sure you still use the base versions of those methods as well to avoid losing any data other controls are saving to the ViewState.
I'm not sure how it works with the methods you are using. I prefer to just override the methods and work directly with the object.
However I would HIGHLY recommend against storing any sensative data, especially passwords, in the ViewState. Passwords should be taken to the server, hashed and compared and nothing more.
-
May 14th, 2008, 01:32 PM
#3
Re: Using ViewState
Use a session variable to hold a user ID or user name. Read that on all the other pages or on the same page itself. You don't need to keep posting it back in viewstate.
-
May 14th, 2008, 01:44 PM
#4
Thread Starter
Addicted Member
Re: Using ViewState
I have a problem using session variable before, after writing to a file, my session variables are no longer there. This is why I'm thinking of using viewstate. Anything else you would recommend? What are the reasons that could cause lost of variables in session?
Thanks!
-
May 14th, 2008, 01:47 PM
#5
Re: Using ViewState
Show the code that you tried to use. How you set it, how you read it.
-
May 14th, 2008, 02:45 PM
#6
Thread Starter
Addicted Member
Re: Using ViewState
First of all, thanks for willing to take a look at this:
This is what I have on the logon page when user click logon, the following session variables are retrieved:
Code:
Protected Sub btnLogon_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim clsRData As New clsRData
Dim clsACData As New clsACData
Dim clsRSum As New clsRSummary
If (clsRData.ValidateUser(getUserName, getPassword, getGroupID)) Then
Session("username") = getUserName()
Session("password") = getPassword()
Session("groupID") = getGroupID()
Session("groupIDwName") = getGroupIDwName()
Session("recruiterID") = clsRSum.getLoginRecruiterID(getUserName())
Session("OpCloseInd") = getOpCloseInd()
Session("logonYear") = Left(getLogonYear(), 4)
Session.Timeout = 240
Response.Redirect("~\ACStart.aspx")
Else
UpdatePanel2.Visible = "true"
lblPassError.Text = "Invalid Password, please try again."
End If
Catch ex As Exception
Throw
End Try
End Sub
Once user logon, they could go to any of the listed pages. One of the pages involve assigning the tables to various people in the database. The tables data come from 4 text files so I endedup combine the 4 textfiles and have an Open/Close indicator to mark whether the table is assigned. Thus, after the table is assigned, the text file is rewrite. The following is the button_submit click on table assigning page.
Code:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim clsACData As New clsACData
Dim clsRData As New clsRData
Dim groupID As String = Session("groupID")
groupID = Trim(groupID)
Dim retreatDate As String = clsRData.RetreatDate(groupID)
retreatDate = Replace(retreatDate, "/", "")
Dim path As String = My.Request.PhysicalApplicationPath & "App_Code\" & groupID & "_" & retreatDate & "\" & "tempTable1_10.txt"
Dim path2 As String = My.Request.PhysicalApplicationPath & "App_Code\" & groupID & "_" & retreatDate & "\" & "tempTable11_20.txt"
Dim path3 As String = My.Request.PhysicalApplicationPath & "App_Code\" & groupID & "_" & retreatDate & "\" & "tempTable21_30.txt"
Dim path4 As String = My.Request.PhysicalApplicationPath & "App_Code\" & groupID & "_" & retreatDate & "\" & "tempTable31_42.txt"
Dim assignStr, assignStr2, assignStr3, assignstr4 As String()
If File.Exists(path) Then
assignStr = clsRData.GetListOfTempAssignsNoSkip("tempTable1_10", groupID)
Else
assignStr = clsRData.GetListOfAssignsNoSkip("Table1_10")
End If
If File.Exists(path2) Then
assignStr2 = clsRData.GetListOfTempAssignsNoSkip("tempTable11_20", groupID)
Else
assignStr2 = clsRData.GetListOfAssignsNoSkip("Table11_20")
End If
If File.Exists(path3) Then
assignStr3 = clsRData.GetListOfTempAssignsNoSkip("tempTable21_30", groupID)
Else
assignStr3 = clsRData.GetListOfAssignsNoSkip("Table21_30")
End If
If File.Exists(path4) Then
assignstr4 = clsRData.GetListOfTempAssignsNoSkip("tempTable31_42", groupID)
Else
assignstr4 = clsRData.GetListOfAssignsNoSkip("Table31_42")
End If
Dim tempAssignStr As String() = clsRData.JoinArrays(assignStr, assignStr2)
Dim tempAssignStr2 As String() = clsRData.JoinArrays(assignStr3, assignstr4)
Dim finalAssignStr As String() = clsRData.JoinArrays(tempAssignStr, tempAssignStr2)
'Existing TableList in database
Dim assignedTables As String()
assignedTables = clsRData.RetrieveAssignedTables(groupID)
Dim attempAssignedTables(GridView1.Rows.Count) As String
For Each gvr As GridViewRow In GridView1.Rows
'Get a programmatic reference to the TextBox control
Dim inputStr As String = CType(gvr.FindControl("TableNumber"), TextBox).Text
Dim recordIDStr As String = CType(gvr.FindControl("RetreatantID"), Label).Text
Dim tableIDStr As String = CType(gvr.FindControl("TableID"), Label).Text
'check input values against exiting values in db
Dim compareFlag As Integer
For m As Integer = 0 To assignedTables.Length - 1
compareFlag = String.Compare(assignedTables(m), inputStr)
If (compareFlag = 0) Then
m = assignedTables.Length - 1
End If
Next m
If (compareFlag = 0) Then
lblError3.Text = "One or more tables already assigned to a retreatant. Please re-run your retreatant list not assigned a table to designate a new table."
'check existing values agains text file & mark it closed if still avail
For k As Integer = 0 To assignedTables.Length - 1
For l As Integer = 0 To finalAssignStr.Length - 1
Dim compareFlag2 As String
compareFlag2 = String.Compare(assignedTables(k), Mid(finalAssignStr(l), 5))
If (compareFlag2 = 0) Then
finalAssignStr(l) = Replace(finalAssignStr(l), "O", "C")
End If
Next l
Next k
Else
'update database & mark value unavail
clsACData.UpdateTableAssign(recordIDStr, inputStr, tableIDStr)
For n As Integer = 0 To finalAssignStr.Length - 1
If String.Compare(Right(finalAssignStr(n), finalAssignStr(n).Length - 4), inputStr) = 0 Then
finalAssignStr(n) = Replace(finalAssignStr(n), "O", "C")
End If
Next
End If
Next
clsRData.ReWriteTableList(finalAssignStr, groupID)
btnAutoAssign.Visible = "false"
btnCancel.Visible = "false"
btnSubmit.Visible = "false"
GridView1.Visible = False
ListBox1.DataSource = New String() {}
ListBox1.DataBind()
ListBox1.DataSource = clsRData.GetListOfTempAssigns("tempTable1_10", groupID)
ListBox1.DataBind()
ListBox2.DataSource = New String() {}
ListBox2.DataBind()
ListBox2.DataSource = clsRData.GetListOfTempAssigns("tempTable11_20", groupID)
ListBox2.DataBind()
ListBox3.DataSource = New String() {}
ListBox3.DataBind()
ListBox3.DataSource = clsRData.GetListOfTempAssigns("tempTable21_30", groupID)
ListBox3.DataBind()
ListBox4.DataSource = New String() {}
ListBox4.DataBind()
ListBox4.DataSource = clsRData.GetListOfTempAssigns("tempTable31_42", groupID)
ListBox4.DataBind()
End Sub
Everything looks just fine...except after I submit the tables assigned and attempt to go to another page, all my session values that stored from the logon page is lost. Any idea? I know it's hard to predict at this point, but anything is appreciated. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|