Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. making control on run-time from a to z

making control on run-time from a to z

Scheduled Pinned Locked Moved ASP.NET
helptutorial
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J John Kuhn

    First off, learn how to write better pseudocode, so we can tell exactly what you intend to do. Second, my best guess is that you are trying to do this:

    For items As Integer = 1 To 100
    Dim nextLabel As New Label
    nextLabel.Text = "This is control number " + items.ToString()
    nextLabel.ID = "Control" + items.ToString()
    Page.Controls.Add(nextLabel)
    Page.Controls.Add(New LiteralControl("<br>"))
    Next

    What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

    R Offline
    R Offline
    R Thomas 0
    wrote on last edited by
    #5

    yea..i was..tks... but another tougher prob arises when i do as your code... now the values are stored in those textboxes... but now lets say i wanna retrieve all the values when the user clicks a button and put it inside an array... i can write a forloop but then how can i do this for i = 0 to arrthings.count()-1 arr1.add("val" & i, ???)'**how can i put the values of textboxes inside?? '**rangin from txtbx1 to txtbx7? next can u pls tell me how i can do that??? tks a lot.. really appreciate your effort and help... sory for not being articulate.. "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

    J 1 Reply Last reply
    0
    • R R Thomas 0

      yea..i was..tks... but another tougher prob arises when i do as your code... now the values are stored in those textboxes... but now lets say i wanna retrieve all the values when the user clicks a button and put it inside an array... i can write a forloop but then how can i do this for i = 0 to arrthings.count()-1 arr1.add("val" & i, ???)'**how can i put the values of textboxes inside?? '**rangin from txtbx1 to txtbx7? next can u pls tell me how i can do that??? tks a lot.. really appreciate your effort and help... sory for not being articulate.. "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

      J Offline
      J Offline
      John Kuhn
      wrote on last edited by
      #6

      An article here[^] on CP describes how to maintain state for dynamically created controls. The article is in C#, but it's not hard to convert to VB. What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

      R 1 Reply Last reply
      0
      • J John Kuhn

        An article here[^] on CP describes how to maintain state for dynamically created controls. The article is in C#, but it's not hard to convert to VB. What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

        R Offline
        R Offline
        R Thomas 0
        wrote on last edited by
        #7

        the article you gave me is what you already taught me....:) i am looking for the part to retrieve data from the dynamically created controls dynamically... any ideas? "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

        J 1 Reply Last reply
        0
        • R R Thomas 0

          the article you gave me is what you already taught me....:) i am looking for the part to retrieve data from the dynamically created controls dynamically... any ideas? "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

          J Offline
          J Offline
          John Kuhn
          wrote on last edited by
          #8

          Hey, shouting is not necessary! ;) The point of the article is that if you generate some controls dynamically, send it to the user, and the page is posted back to the server, you need to re-generate the same controls with the same ID's in order to access the values of the dynamically created controls in the post back handler. What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

          R 1 Reply Last reply
          0
          • J John Kuhn

            Hey, shouting is not necessary! ;) The point of the article is that if you generate some controls dynamically, send it to the user, and the page is posted back to the server, you need to re-generate the same controls with the same ID's in order to access the values of the dynamically created controls in the post back handler. What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

            R Offline
            R Offline
            R Thomas 0
            wrote on last edited by
            #9

            i am sorry, i wasnt shouting..:( i got it already.... you already told me that... now the thing is i want to retrieve the values... all is done, but my values are stuck....:( i cant seem to get it back dynamically...:( these are my code. ' This routine creates the controls and assigns a generic ID Private Sub createControls(ByVal arrList As ArrayList) Dim count As Integer = Me.NumberOfControls Dim i As Integer For i = 0 To count Dim myLabel As Label = New Label ' Set the label's Text and ID properties. myLabel.Text = arrList.Item(i) & " : " myLabel.ID = "Label_" & i.ToString() Page.Controls.Add(myLabel) 'Add a spacer in te form of an HTML     element Dim tab As LiteralControl = New LiteralControl("     ") Dim myTextBox As TextBox = New TextBox 'Set the textbox's Text and ID properties myTextBox.Text = "" myTextBox.ID = "TextBox_" & i.ToString() Page.Controls.Add(myTextBox) ' Add a spacer in the form of an HTML element Dim spacer As LiteralControl = New LiteralControl(" ") Page.Controls.Add(spacer) Next 'Session.Add("NoOfRecords", i) 'Dim j As Integer 'Dim temp As String = ("label" & i.ToString()) & ".Text" 'i.ToString() myPlaceHolder.Visible = True lblInfo.Visible = True imgBtnSelectReport.Visible = True imgBtnRetrieveReport.Visible = True Session.Add("FilterArray", arrList) ddlReportCat.Enabled = False End Sub 'createControls i am trying to write a for loop to access all the caontrol values.. but i dont know how many vales are there to access... :( i dont seem to be able to write the for loop... can you pls help... tks a lot... "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

            R 1 Reply Last reply
            0
            • R R Thomas 0

              i am sorry, i wasnt shouting..:( i got it already.... you already told me that... now the thing is i want to retrieve the values... all is done, but my values are stuck....:( i cant seem to get it back dynamically...:( these are my code. ' This routine creates the controls and assigns a generic ID Private Sub createControls(ByVal arrList As ArrayList) Dim count As Integer = Me.NumberOfControls Dim i As Integer For i = 0 To count Dim myLabel As Label = New Label ' Set the label's Text and ID properties. myLabel.Text = arrList.Item(i) & " : " myLabel.ID = "Label_" & i.ToString() Page.Controls.Add(myLabel) 'Add a spacer in te form of an HTML     element Dim tab As LiteralControl = New LiteralControl("     ") Dim myTextBox As TextBox = New TextBox 'Set the textbox's Text and ID properties myTextBox.Text = "" myTextBox.ID = "TextBox_" & i.ToString() Page.Controls.Add(myTextBox) ' Add a spacer in the form of an HTML element Dim spacer As LiteralControl = New LiteralControl(" ") Page.Controls.Add(spacer) Next 'Session.Add("NoOfRecords", i) 'Dim j As Integer 'Dim temp As String = ("label" & i.ToString()) & ".Text" 'i.ToString() myPlaceHolder.Visible = True lblInfo.Visible = True imgBtnSelectReport.Visible = True imgBtnRetrieveReport.Visible = True Session.Add("FilterArray", arrList) ddlReportCat.Enabled = False End Sub 'createControls i am trying to write a for loop to access all the caontrol values.. but i dont know how many vales are there to access... :( i dont seem to be able to write the for loop... can you pls help... tks a lot... "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

              R Offline
              R Offline
              R Thomas 0
              wrote on last edited by
              #10

              my prob is that i cant get the values from the generated controls... i know the no of controls that are generated.. i know that i want the values of the textboxes generated but i cant get the values... :( lets say i have TextBox_1 TextBox_2 TextBox_3 TextBox_4 i know the number of the textboxes so i am able to address them but when it comes to retrieve the values comes the prob.. lets say i write the loop to retrieve the values for i=0 to NoOfControls() dim ans as string = '*Here i want the value of TextBox_1.Text, TextBox_2.Text,TextBox_3.Text,TextBox_4.Text 'but i cant seem to assign it that way.. dim ans as string = ("TextBox_" + i).Text ???? it gives me error :( 'pls help... next hope you can help.... tks "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

              J 1 Reply Last reply
              0
              • R R Thomas 0

                my prob is that i cant get the values from the generated controls... i know the no of controls that are generated.. i know that i want the values of the textboxes generated but i cant get the values... :( lets say i have TextBox_1 TextBox_2 TextBox_3 TextBox_4 i know the number of the textboxes so i am able to address them but when it comes to retrieve the values comes the prob.. lets say i write the loop to retrieve the values for i=0 to NoOfControls() dim ans as string = '*Here i want the value of TextBox_1.Text, TextBox_2.Text,TextBox_3.Text,TextBox_4.Text 'but i cant seem to assign it that way.. dim ans as string = ("TextBox_" + i).Text ???? it gives me error :( 'pls help... next hope you can help.... tks "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

                J Offline
                J Offline
                John Kuhn
                wrote on last edited by
                #11

                Dude, you're forcing me to write a bunch of code to show you how this works. ;)

                Public Class DynamicControlsPage
                Inherits System.Web.UI.Page

                ' details omitted...
                
                ' Modifications of Mads Nissen's code for article "Retaining State for Dynamically Created Controls in ASP.NET applications" 
                Private Sub Page\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                    If Not IsPostBack Then
                        NumberOfControls = 0
                    Else
                        ReCreateControls()
                    End If
                End Sub
                
                Protected Property NumberOfControls() As Integer
                    Get
                        Return CType(ViewState("NumControls"), Integer)
                    End Get
                    Set(ByVal Value As Integer)
                        ViewState("NumControls") = Value
                    End Set
                End Property
                
                Private Sub ReCreateControls()
                    For i As Integer = 1 To Me.NumberOfControls
                        Dim tx As New TextBox
                        tx.ID = "TextBox\_" + i.ToString()
                        phTextBoxes.Controls.Add(tx)
                    Next
                End Sub
                
                Private Sub cmdAddControls\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddControls.Click
                    For i As Integer = 1 To 10
                        Dim tx As New TextBox
                        tx.ID = "TextBox\_" + i.ToString()
                        phTextBoxes.Controls.Add(tx)
                        NumberOfControls += 1
                    Next
                End Sub
                
                Private Sub cmdDisplayValueFive\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisplayValueFive.Click
                    Dim tx As TextBox = CType(Page.FindControl("TextBox\_5"), TextBox)
                    txtValueDisplay.Text = tx.Text
                End Sub
                

                End Class

                So, the answer to your question lies in the cmdDisplayValueFive_Click method outlined above. What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

                R 1 Reply Last reply
                0
                • J John Kuhn

                  Dude, you're forcing me to write a bunch of code to show you how this works. ;)

                  Public Class DynamicControlsPage
                  Inherits System.Web.UI.Page

                  ' details omitted...
                  
                  ' Modifications of Mads Nissen's code for article "Retaining State for Dynamically Created Controls in ASP.NET applications" 
                  Private Sub Page\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                      If Not IsPostBack Then
                          NumberOfControls = 0
                      Else
                          ReCreateControls()
                      End If
                  End Sub
                  
                  Protected Property NumberOfControls() As Integer
                      Get
                          Return CType(ViewState("NumControls"), Integer)
                      End Get
                      Set(ByVal Value As Integer)
                          ViewState("NumControls") = Value
                      End Set
                  End Property
                  
                  Private Sub ReCreateControls()
                      For i As Integer = 1 To Me.NumberOfControls
                          Dim tx As New TextBox
                          tx.ID = "TextBox\_" + i.ToString()
                          phTextBoxes.Controls.Add(tx)
                      Next
                  End Sub
                  
                  Private Sub cmdAddControls\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddControls.Click
                      For i As Integer = 1 To 10
                          Dim tx As New TextBox
                          tx.ID = "TextBox\_" + i.ToString()
                          phTextBoxes.Controls.Add(tx)
                          NumberOfControls += 1
                      Next
                  End Sub
                  
                  Private Sub cmdDisplayValueFive\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisplayValueFive.Click
                      Dim tx As TextBox = CType(Page.FindControl("TextBox\_5"), TextBox)
                      txtValueDisplay.Text = tx.Text
                  End Sub
                  

                  End Class

                  So, the answer to your question lies in the cmdDisplayValueFive_Click method outlined above. What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

                  R Offline
                  R Offline
                  R Thomas 0
                  wrote on last edited by
                  #12

                  really really appreciate your help and your effort Mr.John... one last question pls if i do this : dim tx as TextBox = CType(Page.FindControl("TextBox_" & i.ToString),TextBox) will it still work?? tks a lot... Mr.John... :) really really appreciate your help... :) tk you so much..:) "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

                  J 1 Reply Last reply
                  0
                  • R R Thomas 0

                    really really appreciate your help and your effort Mr.John... one last question pls if i do this : dim tx as TextBox = CType(Page.FindControl("TextBox_" & i.ToString),TextBox) will it still work?? tks a lot... Mr.John... :) really really appreciate your help... :) tk you so much..:) "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

                    J Offline
                    J Offline
                    John Kuhn
                    wrote on last edited by
                    #13

                    Yup, that's the general idea. Happy coding! What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

                    R 1 Reply Last reply
                    0
                    • J John Kuhn

                      Yup, that's the general idea. Happy coding! What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

                      R Offline
                      R Offline
                      R Thomas 0
                      wrote on last edited by
                      #14

                      tks Mr.Kuhn.... appreciate your help :) "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups