using Session variables for arraylist in TEXTBOXES

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adarshyam
    New Member
    • Oct 2008
    • 31

    using Session variables for arraylist in TEXTBOXES

    hi.. I am new to vb.net i am doing a program using dynamic textboxes which involves two pages .. where values of textboxes in one page must be transfered to to other sex of textboxes in another page, . please help me .. I tried using session variables but its not working.. this s my code..

    PAGE 1
    Code:
     Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
    
    nval2 = TextBox2.Text
    
                If numofper > nval2 Then
                    Session("a1") = textdynamic5
                    SessionHandler.keynval2 = nval2
                    Response.Redirect("fortpage.aspx")
    
                Else
                    MsgBox("'n' value must be less than Number of Periods")
                End If
    
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    
    If Session("weights") = True Then
                ReDim TxtDyn5(nval2)
                Button4.EnableViewState = True
                Button4.ID = "btn4"
                For m As Integer = 0 To nval2 - 1
                    TxtDyn5(m) = New TextBox
                    TxtDyn5(m).EnableViewState = True
                    Panel4.Controls.Add(TxtDyn5(m))
                    textdynamic5.Add(textdynamic5)
                    TxtDyn5(m).Width = 60
    
                Next
                Session("a1") = True
            End If
    
    
        End Sub
    PAGE 2
    Code:
    Dim textdynamic5 As New ArrayList
        Dim TxtDyn5 As TextBox()
    
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    Dim textdynamic5 As ArrayList = CType(HttpContext.Current.Session("a1"), ArrayList)
    
     ElseIf Session("WMA") = True Then
                Label4.Text = "WEIGHTED MOVING AVERAGE"
    
                Dim nval2 As String = SessionHandler.keynval2
                Label6.Text = "" + nval2
                Label7.Text = "Value of 'n'"
                Label8.Text = "Weights"
                If Session("weights") = True Then
    
                    ReDim TxtDyn5(nval2)
                    textdynamic5 = Session("a1")
                    For m As Integer = 0 To nval2 - 1
                        TxtDyn5(m) = New TextBox
                        TxtDyn5(m).EnableViewState = True
                        Panel14.Controls.Add(TxtDyn5(m))
                        textdynamic5.Add(textdynamic5)
                        TxtDyn5(m).Width = 60
    
                    Next
    
                End If
    end sub
    please temme wher im goin wrong..
    Last edited by Curtis Rutland; Oct 28 '08, 08:29 PM. Reason: added [CODE] tags
  • stoogots2
    New Member
    • Sep 2007
    • 77

    #2
    adarshyam,

    I didn't look at your code, but I will give you the first place to look when troubleshooting controls that were added dynamically.

    When you add controls dynamically, remember that you have to recreate the controls on postback specifically in OnInit in order to have the ViewState for those controls loaded (ViewState should contain your textbox values that the user typed). So viewstate should be turned on for those controls, and the ID property for each control must be the same when you add it on Page_Load, and recreate/readd it on OnInit.

    Hope that helps.

    Comment

    • adarshyam
      New Member
      • Oct 2008
      • 31

      #3
      thank u v much for your reply.. i have one more doubt.. while recreating the controls in next page can i use same name to those controls? and should i keep the arraylist also in session and get back in next page? if so can u pls temme how..

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by adarshyam
        thank u v much for your reply.. i have one more doubt.. while recreating the controls in next page can i use same name to those controls?
        Sure!
        This is probably advisable because it'll help you understand what is what.

        What I don't understand is why you need to name the controls if they are in an arraylist??


        Originally posted by adarshyam
        and should i keep the arraylist also in session and get back in next page? if so can u pls temme how..
        Check out the article on how to use dynamic controls in ASP.NET

        -Frinny

        Comment

        • adarshyam
          New Member
          • Oct 2008
          • 31

          #5
          ok i ve refined my question tis tym :)

          i need to bring the values of textboxes from page1 to page2

          page1

          ' i ve created textboxes using a button n put the textboxes in viewstate in pageload with same ID. i ve given the session of arraylist in the goto next page button

          pageload
          [code=vbnet]
          If Session("weight s") = True Then
          Dim textdynamicwt As New ArrayList()
          Dim TxtDynwt As TextBox()
          ReDim TxtDynwt(nval2)
          Button4.EnableV iewState = True
          Button4.ID = "btn4"
          For m As Integer = 0 To nval2 - 1
          TxtDynwt(m) = New TextBox
          TxtDynwt(m).Ena bleViewState = True
          Panel4.Controls .Add(TxtDynwt(m ))
          textdynamicwt.A dd(textdynamicw t)
          TxtDynwt(m).Wid th = 60
          TxtDynwt(m).ID = "TxtDynwt_" & m
          Next

          End If
          [/code]
          button(goto next page)
          [code=vbnet]
          If Session("weight s") = True Then
          Session("txtdyn ") = textdynamicwt
          [/code]
          page 2

          ' i ve created dyn textboxes again and retrieved arraylist from session

          pageload
          [code=vbnet]
          textdynamicwt = Session("txtdyn ")
          If Session("weight s") = True Then
          Dim textdynamicwt As New ArrayList
          Dim TxtDynwt As TextBox()
          ReDim TxtDynwt(nval2)
          For m As Integer = 0 To nval2 - 1
          TxtDynwt(m) = New TextBox
          TxtDynwt(m).Ena bleViewState = True
          Panel14.Control s.Add(TxtDynwt( m))
          textdynamicwt.A dd(textdynamicw t)
          TxtDynwt(m).Wid th = 60
          TxtDynwt(m).ID = "TxtDynwt_" & m
          Next
          End If
          [/code]
          Now my problem is i can see only empty textboxes in page2 i cant get the values that i entered in the textboxes in page1. please help me with a solution i am trying this for the past two days.. im very new to vb.net...
          Last edited by Frinavale; Oct 31 '08, 05:47 PM. Reason: added [code] tags

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Did you read the article on how to use dynamic controls that I posted earlier?
            You cannot add your TextBoxes to the page in the PageLoad event. You have to do that in the OnInit event....

            Please read the article for why.
            This will explain why you aren't getting any text values.

            In the future please remember to use code tags when posting code snippets.

            -Frinny

            Comment

            • adarshyam
              New Member
              • Oct 2008
              • 31

              #7
              sorry i forgot to use code tags and thanks for the guidance..

              Comment

              • adarshyam
                New Member
                • Oct 2008
                • 31

                #8
                frinny,
                can you please send me the link for the article u were talkin about..i couldnt find it in the forum.. i see webform_Init event.. is that the same as OnInit??

                Comment

                • Curtis Rutland
                  Recognized Expert Specialist
                  • Apr 2008
                  • 3264

                  #9
                  Here's Frinny's article:

                  Comment

                  Working...