loop through dynamic form fields

Last post 12-30-2006 11:56 PM by mi32dogs. 7 replies.

Sort Posts:

  • loop through dynamic form fields

    12-30-2006, 2:51 PM
    • Member
      1 point Member
    • mi32dogs
    • Member since 12-14-2006, 12:33 PM
    • Posts 11

    Here is my problem

    I have a form with 12 rows with all the same fields (row 1 has textbox1, checkbox1, etc.. Row 2 has textbox2, checkbox2, etc..)

    If the form gets submitted I like to loop through the form data but how do I call the form data
    for example checkbox+Z.Checked dos not work  in JS you can use eval() but that dos not work

    For Z = 1 To 12

    If checkbox+Z.Checked Then
                    ‘Do something
    End If

    Next

  • Re: loop through dynamic form fields

    12-30-2006, 3:27 PM
    • All-Star
      98,112 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs

    you could try something like this:

        For Z As Integer = 1 To 12
          Dim controlName As String = "Checkbox" & Z.ToString
          Dim thisCheckbox As CheckBox = DirectCast(Page.FindControl(controlName), CheckBox)
          If Not thisCheckbox Is Nothing AndAlso thisCheckbox.Checked = True Then
            'Do something
          End If    
        Next
    
     
    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: loop through dynamic form fields

    12-30-2006, 5:18 PM
    • Member
      1 point Member
    • mi32dogs
    • Member since 12-14-2006, 12:33 PM
    • Posts 11

    Hi Thanks but if do this i get a "Object reference not set to an instance of an object." Error
    This is part of the script

      
    For Z = 1 To 12
    Dim controlName As String = "ShowID" & Z.ToString
    Dim ShowID As DropDownList = DirectCast(Page.FindControl(controlName), DropDownList)

    controlName = "Onsale" & Z.ToString
    Dim Onsale As CheckBox = DirectCast(Page.FindControl(controlName), CheckBox)

    controlName = "PreStart" & Z.ToString
    Dim PreStart As BasicFrame.WebControls.BDPLite = DirectCast(Page.FindControl(controlName), BasicFrame.WebControls.BDPLite)

    controlName = "STime" & Z.ToString
    Dim STime As TextBox = DirectCast(Page.FindControl(controlName), TextBox)

    controlName = "PreEnd" & Z.ToString
    Dim PreEnd As BasicFrame.WebControls.BDPLite = DirectCast(Page.FindControl(controlName), BasicFrame.WebControls.BDPLite)

    controlName = "ETime" & Z.ToString
    Dim ETime As TextBox = DirectCast(Page.FindControl(controlName), TextBox)

    controlName = "Password" & Z.ToString
    Dim Password As TextBox = DirectCast(Page.FindControl(controlName), TextBox)

    controlName = "SDate" & Z.ToString
    Dim SDate As CheckBox = DirectCast(Page.FindControl(controlName), CheckBox)

    'test if field is empty If ShowID.Text.ToString <> "0" Then 'here i'm getting the "Object reference not set to an instance of an object." Error 'Do something End If
    Next
     
     

     

  • Re: loop through dynamic form fields

    12-30-2006, 5:28 PM
    • All-Star
      98,112 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs

    Check to see if ShowId is Nothing.

    Findcontrol only finds controls inside the naming container from which findcontrol is called.

    You could try replacing Page.FindControl with Me.FindControl if these controls are not directly on the page.

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: loop through dynamic form fields

    12-30-2006, 9:27 PM
    • Member
      1 point Member
    • mi32dogs
    • Member since 12-14-2006, 12:33 PM
    • Posts 11

    I tried it with Me.FindControl but i get the same error

    I also tested ShowID1 to see if it is Nothing  but if i use ShowID1.Text i get the value so thats fine

    I'm not sure why it is not working
     

     

  • Re: loop through dynamic form fields

    12-30-2006, 9:57 PM
    • Member
      1 point Member
    • mi32dogs
    • Member since 12-14-2006, 12:33 PM
    • Posts 11
    Ok, I found the problem i use master pages so i have to use
    Dim ShowID As DropDownList = DirectCast(Master.FindControl("Content").FindControl(controlName), DropDownList)
     I found this http://west-wind.com/weblog/posts/5127.aspx
  • Re: loop through dynamic form fields

    12-30-2006, 10:33 PM
    Answer
    • All-Star
      98,112 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs

    Yes - each placeholder declared on the masterpage represents a namingcontainer.

    Findcontrol can only find controls declared within the naming container from which findcontrol is called.

    So with a master page you get

    Page 
       --- Master Page
           --- Master Page "Content" Placeholder 
               --- Your content

    Smile Dont forget to close out your thread if you're all set.  You can mark it as "Resolved" at the top if there is no individual post that represents your answer.  Or if you consider any reply to be your answer, you can click the "Mark As Answer" button for that specific post.  This action also sets the thread status to "Resolved"

    Welcome to the forum.

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: loop through dynamic form fields

    12-30-2006, 11:56 PM
    • Member
      1 point Member
    • mi32dogs
    • Member since 12-14-2006, 12:33 PM
    • Posts 11
    Thanks for all the help
Page 1 of 1 (8 items)