Dynamic Checkboxes? Or a different way all together

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ryushinyama
    New Member
    • Feb 2007
    • 23

    #1

    Dynamic Checkboxes? Or a different way all together

    New to asp.net. Pretty familiar with vb.net. This maybe a simple question but I can't seem to find a good answer to it anywhere or at least an alternative/better way.

    I am trying to dynamically create checkboxes for items that need to be selected and updated in the database.

    The code below will allow those checkboxes to be created and display on the web form and work correctly. However, when I try to access them from another function/Sub I can not.

    Code:
    Private Sub GetData( orderNumber as integer)
    
            sqlQueryString = "exec OrderItems " & orderNumber
    
            Dim objDs As DataSet
            Dim objSQLDa As SqlDataAdapter
            Dim objRow As DataRow
    
            objDs = New DataSet()
    
            objSQLDa = New SqlDataAdapter(sqlQueryString, ConnString)
    
            objSQLDa.Fill(objDs, "Dropship Data")
    
    
            Dim chk(objDs.Tables(0).Rows.Count) As CheckBox
            i = 0
    
            For Each objRow In objDs.Tables(0).Rows
    
                sItemID = "<strong>ItemID:</strong> " & objRow("ItemID").ToString & "<br>" & CrLf
                sBrand = "<strong>Brand:</strong> " & objRow("Brand").ToString & "<br>" & CrLf
                sDropShip = "<strong>Dropship:</strong> " & objRow("dropship").ToString & "<br>" & CrLf
    
                chk(i) = New CheckBox
    
                chk(i).ID = objRow("id").ToString
                chk(i).Text = sItemID & sBrand & sDropShip
    
                Me.productData.Controls.Add(chk(i))
                i += i
            Next
    End Sub
    Now I can't get the data back after submitting the form. It is really easy to work with the controls that are created in the design view. But I don't know how to work with the controls created in the code.

    I would like to check the boxes that are needed. Click a submit button. Then have the click button event handler loop through the ID's for the checked boxes and process a stored proc that will do all the updates in the DB that need to be done.

    Something like:

    Code:
    Sub DoThisWhenTheButtonIsPressed()
       
       For i=0 to uBound(chk)
    		if chk(i).checked then
    			DoStoredProc(chk(i).ID)
    		end if
       Next
    
    End Sub
    It is really easy to work with controls that are created in the design view. I just can't find out how to get the data from the controls that are created in the code.

    Also is there a method for view all the post back data from the form? Something like:

    response.write me.form1.postba ckdata


    Thanks for your help!
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    #2
    Hope this will help

    This article demonstrates how you can dynamically generate ASP.NET controls in runtime using the code behind code and access the properties of the dynamically created controls.

    Comment

    • ryushinyama
      New Member
      • Feb 2007
      • 23

      #3
      Its like someone back in 2004 was thinking, "Hey, this guy is gonna come looking for this later. I'd better get it up now."

      Exactly what I was looking for, thanks!!

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        The code posted in the article that you've been linked to will probably work for now but you could possibly run into problems in the future because you are instantiating the control in your Page Load event. You should instead move this code to your Page Init event. The reason is outlined in this article about how to use dynamic controls in ASP.NET

        -Frinny

        Comment

        Working...