Results 1 to 9 of 9

Thread: How to ensure the form user selections when user decides going back?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70

    How to ensure the form user selections when user decides going back?

    I have seen several web sites that in web forms user registration that consists with more than one page, when user presses the browser back button in order to go to the previous form filled page, seems that the previous page is rebuilt to ensure to fill all the form fields options according to the user selections. Seems that they rebuilt the page instead of rely on the user browser cache in order to maintain the options selected when user decides going back. Is this practice true? Or they are doing other things when the previous page is reloaded?

    I have some dropdownlist form fields that are populated on client side, and these controls doesn’ t keep the original selections when the user decides to return to the form through the browser’s back button.

    Any ideas?


    Thanks

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    A few things they could be doing:
    1) storing the entered values into a proxy table in SQL, and relying on that table to refill fields.

    2) Reposting to the same page, and hiding/showing panels on that page that relate to what step in the registration the user is in.

    3) passing a dataset between the forms using viewstate, and retrieving the data related to the current page from the dataset.

    On the last page, they use the fully-formed dataset and pass it to a procedure that completes the registration.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    Now I already store the form control values to a session variable, with a table into a dataset, and at the end of the process I Insert all the values in the database. Is this a good way of collecting the information during the process registration? Or there is another faster?

    But I have to find out how to write a subroutine into the 'Page_Load' so that only is triggered if the form controls are stored in the current session table variables. To assign the session table variables values to the controls it' s easy, but I don' t know how to write the decision of triggering or not the subroutine according to whether the session table values are present or not. For instance:

    Code:
    Sub Page_Load (Sender As Object, E As EventArgs)
    
       Dim UserDS As Data.DataSet = Session("UserData") 
       If (UserDS.Tables(0).rows(0).Item("City")) <> null Then
    
          Control1.SelectedItem.Value = UserDS.Tables(0).rows(0).Item("City")
          Control2.SelectedItem.Value = UserDS.Tables(0).rows(0).Item("Sate")
          Control3.SelectedItem.Value = UserDS.Tables(0).rows(0).Item("Activity")   
          Control4.Text() = UserDS.Tables(0).rows(0).Item("Name")  
          Control4.Text() = UserDS.Tables(0).rows(0).Item("Surname")
          ...
    
        End If
    
     End Sub
    The line highlighted in bold is not correct. Do you know how can I write the condition so that only the values will be filled if the control values of the current page are already stored on the current session variable? Because if the user arrives to the form for the first time I don’ t want (and I can’ t) to fill the controls with values from the current session.

    Thank you,
    Cesar

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Hmm....

    How many fields are you storing? I see city, state, zip, activity, surname... it helps to know how many you have to store for this 'registration'... 20, 40, 50?

    Because if its not an exuberant amount, I might be tempted to just store them in a dataset in viewstate, and pass that along via server.transfer, forward or back from the current page...

    Also, if its only about 2 or 3 pages, you might be even better off using just one page.

    Obviously, the registration in both scenarios above simply require a user hit a SUBMIT button, which resides on the last page, to trigger the database save.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    In total I have 30 fields, 15 per page more or less. and I prefer to use three pages (separate steps) for several reasons.
    Because if its not an exuberant amount, I might be tempted to just store them in a dataset in viewstate, and pass that along via server.transfer, forward or back from the current page...
    Would you mind teaching me this technique of to store the data fields in a dataset in viewstate, and that along via server.transfer, forward or back from the current page…? As well some information on the internet if it’ s possible.

    As I said I need to know how to write the condition so that only the values will be filled if the control values of the current page are already stored in the current DataSet.....


    Thank you

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Here you go, page 2 from my deceased vehicle wizard in my web app.. some importants part of it are bolded...
    VB Code:
    1. 'this is the 2nd page in a series of 4
    2. 'but it illustrates how to handle
    3. 'data coming either from the first page (page 1)
    4. 'or page 3 (if the user hits previous on page 3)
    5. Public Class VehicleWiz2
    6.     Inherits System.Web.UI.Page
    7.  
    8. #Region " Web Form Designer Generated Code "
    9.  
    10.     'This call is required by the Web Form Designer.
    11.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    12.  
    13.     End Sub
    14.  
    15.     Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    16.     Protected WithEvents tbVIN As System.Web.UI.WebControls.TextBox
    17.     Protected WithEvents tbVehicleModel As System.Web.UI.WebControls.TextBox
    18.     Protected WithEvents tbVehicleYear As System.Web.UI.WebControls.TextBox
    19.     Protected WithEvents ddlVehicleMake As System.Web.UI.WebControls.DropDown
    20.     Protected WithEvents ddlVehicleStyle As System.Web.UI.WebControls.DropDown
    21.     Protected WithEvents ddlVehicleColor As System.Web.UI.WebControls.DropDown
    22.     Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox
    23.     Protected WithEvents btnPrev As System.Web.UI.WebControls.Button
    24.     Protected WithEvents btnNext As System.Web.UI.WebControls.Button
    25.  
    26.     'NOTE: The following placeholder declaration is required by the Web Form Designer.
    27.     'Do not delete or move it.
    28.     Private designerPlaceholderDeclaration As System.Object
    29.  
    30.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    31.         'CODEGEN: This method call is required by the Web Form Designer
    32.         'Do not modify it using the code editor.
    33.         InitializeComponent()
    34.     End Sub
    35.  
    36. #End Region
    37.  
    38. [b]'this allows other pages to retrieve dataset from
    39.     'this page
    40.     Public ReadOnly Property Vehicle() As String
    41.         Get
    42.             Return Me.ViewState("vehicledata")
    43.         End Get
    44.     End Property
    45. [/b]
    46.  
    47.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    48.         'Put user code to initialize the page here
    49.         Dim _vehicledataset As New DataSet
    50.         If Not IsPostBack Then
    51.  
    52.            [b] If TypeOf context.Handler Is VehicleWiz1 Then
    53.                 'came from page 1
    54.                 Dim v1 As VehicleWiz1 = DirectCast(context.Handler, VehicleWiz1)
    55.                 _vehicledataset.ReadXml(New System.IO.StringReader(v1.Vehicle), XmlReadMode.ReadSchema)
    56.                 v1.Dispose()
    57.  
    58.             Else
    59.                 'came from page 3
    60.                 Dim v3 As VehicleWiz3 = DirectCast(context.Handler, VehicleWiz3)
    61.                 _vehicledataset.ReadXml(New System.IO.StringReader(v3.Vehicle))
    62.                 v3.Dispose()
    63.  
    64.             End If
    65. [/b]
    66.             Me.tbVehicleModel.Text = _vehicledataset.Tables(0).Rows(0).Item(13)
    67.             Me.tbVehicleYear.Text = _vehicledataset.Tables(0).Rows(0).Item(9)
    68.             Me.tbVIN.Text = _vehicledataset.Tables(0).Rows(0).Item(7)
    69.             Me.ddlVehicleColor.SelectedValue = _vehicledataset.Tables(0).Rows(0).Item(12)
    70.             Me.ddlVehicleMake.SelectedValue = _vehicledataset.Tables(0).Rows(0).Item(11)
    71.             Me.ddlVehicleStyle.SelectedValue = _vehicledataset.Tables(0).Rows(0).Item(10)
    72.            
    73.             'add the dataset to this page's viewstate
    74.             'which is useful if we postback to the same page for some reason
    75.            [b] Dim sw As New System.IO.StringWriter
    76.             _vehicledataset.WriteXml(sw, XmlWriteMode.WriteSchema)
    77.             Me.ViewState.Add("vehicledata", sw.ToString)
    78. [/b]
    79.         End If
    80.  
    81.     End Sub
    82.  
    83.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    84. [b]
    85.         'scrape data off page, store in dataset
    86.         getupdatedData()
    87.  
    88.         'move to next page
    89.         Server.Transfer("VehicleWiz3.aspx")
    90. [/b]
    91.  
    92.     End Sub
    93.  
    94.     Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
    95.    [b]     'scrape data off page, store in dataset
    96.         getupdatedData()
    97.  
    98.         'move to previous page
    99.         Server.Transfer("VehicleWiz1.aspx")
    100.     [/b]End Sub
    101.  
    102.  
    103.     'this method gets the current data on the page, and puts it in the dataset
    104.     Private Sub getupdatedData()
    105.         Dim _vehicledataset As New DataSet
    106.         _vehicledataset.ReadXml(New System.IO.StringReader(Vehicle), XmlReadMode.ReadSchema)
    107.         With _vehicledataset.Tables(0).Rows(0)
    108.             .Item(13) = tbVehicleModel.Text
    109.             .Item(9) = tbVehicleYear.Text
    110.             .Item(7) = tbVIN.Text
    111.             .Item(12) = ddlVehicleColor.SelectedValue
    112.             .Item(11) = ddlVehicleMake.SelectedValue
    113.             .Item(10) = ddlVehicleStyle.SelectedValue
    114.  
    115.         End With
    116.         Dim sw As New System.IO.StringWriter
    117.         _vehicledataset.WriteXml(sw, XmlWriteMode.WriteSchema)
    118.         Me.ViewState("vehicledata") = sw.ToString
    119.     End Sub
    120.  
    121.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    122.  
    123.         'this responds to a cancel button, which closes the window
    124.         'in my case it was a popup wizard, so this made sense
    125.         Response.Redirect("../CLOSEME.ASPX")
    126.  
    127.     End Sub
    128. End Class

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    Sorry but I am not still prepared to understand and to apply your code, so I will use my system. Anyway I will be been thankful to receive your opinion about my code.

    To store the data filled in the form I use this subroutine:
    (This is the second form page of three)
    Code:
    Sub userData(Sender As Object, e As EventArgs)
      If (Page.IsValid) Then
       
       Dim ds2 As DataSet
       ds2 = CType(Session("UserData"), DataSet)  
     
       Dim row As DataRow
       row = ds2.Tables(0).Rows(0)
       row.Item("companyName") = name.Text
       row.Item("State") = state.SelectedItem.Value
       row.Item("city") = city.SelectedItem.Value
       ...
       
       Session("UserData") = ds2
    
       response.Redirect("Form3_NewUser.aspx")
      End If
    End Sub

    Then to refill the fields with the user selections when the user decides come back to this second form page from the third page with a link.. :
    Code:
    Sub Page_Load(sender As Object, e As EventArgs)
      If Not IsPostBack Then
    
         Dim ds2 As DataSet
         ds2 = CType(Session("UserData"), DataSet)
            If Not isNothing(ds2.tables(0).rows(0)) then
        	
                   name.Text() = ds2.Tables(0).rows(0).Item("companyName").ToString
                   state.SelectedItem.Value = ds2.Tables(0).rows(0).Item("State").ToString     
                   city.SelectedItem.Value = ds2.Tables(0).rows(0).Item("city").ToString     
                   ...     
    	 
         End If
    
      End If
    End Sub

    What do you think about this system of assigning a dataset to a session variable, and using a table to store the data? Is there an easy alternative of using a table to store the data? Because indeed I always only use one row, and it doesn’t seems a logical way of using a table...


    Thank you

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You can certainly use a session variable. I always just stored a username, userguid, firstname and lastname of the session's user in session, and that was all I ever felt comfortable storing.

    Now, you can certainly use session, just remember that whatever you store in there, stay alives through the current session, or until you explicity remove it, so anytime someone uses your app, it will use more and more server memory. For me, if my app had 1000+ users using it at one time, I'd be cautious. Again, I don't know what load you are expecting, so it might just be a tremendously mute point.

    Anyway, you don't have to use a table, although it certainly is easy to write a dataset isn't it?

    You could make a class and just be sure its properties serialize to strings when called to.

    You could make a method SerializeMe() which returns an xml formatted string, which certainly makes it easy to store.

    Or you may be able to get by on .Net's serializing abilities. You won't know until you test it, so good luck.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    Ok, thank you very much nemaroller !

    I see that I have to consider seriously to learn to create a 'simple' struct or class in order to save server resources (my website will be basically to enter information and retrieve it), and I hope to have many users at the same time , despite the app would have a few users if one day starts to increase them it is better to have the application prepared and do not bother to user.

    Too much data in the session variables and datasets consumes many server resources. Until now I have never created a class, I only have read two chapters of my manual about how to built an object or class. It seems very difficult… but I will try it.

    Thanks again.

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