Results 1 to 1 of 1

Thread: Get data from page to know how many dynamic controls to load to grab values

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Get data from page to know how many dynamic controls to load to grab values

    I currently have a page that has 2 panels. First panel has a textbox and a button, second panel has 3 textbox and a button but this panel is not visible intially.

    On load, the user needs to enter a unique code and click the button. On first panel submit, if code is valid, hide the first panel and show second panel which has the three textbox, submit button and a placeholder control.

    In the admin of my site, we can identify for each client if they need to ask additional questions to get more info from their users. Some have none some have 1 or 2+ questions. End result, can have 0 to many questions.

    So back on my form, for each question for that client, I am creating a textbox in the second panel placehoder control.

    My problem is on submit of second panel, to capture the data of these dynamic controls, I can't get my controls to reload because loading the controls requires dynamic data of a code. I can get dynamic controls to work if I have a hard coded value and loop through to recreate. But pulling from the database I can't seem to get to work because on initialize I can't grab the unique code from the form. That unique code determines what client to associate with, which determines how many textbox controls to create/read.

    Is there a better way handling creating dynamic controls based on a value stored/entered in the same page set textbox control?

    HTML Code:
    <asp:Panel ID="pnlAccessCode" runat="server">
        <fieldset class="access-code-form">
            <legend></legend> 
            <div>
                <label for="txtAccessCode" class="required">Access Code</label> <asp:TextBox ID="txtAccessCode" ToolTip="Access Code" 
                    runat="server" MaxLength="100" />
           </div>
           <div>
                <label for="btnACRegister">&nbsp;</label>
                <asp:Button ID="btnACRegister" runat="server" Text="Register" />
           </div>
    
         </fieldset>
    </asp:Panel>
    
    <asp:Panel ID="pnlInfo" runat="server" Visible="false">
        <fieldset>
            <legend></legend>
            <div>
                <label for="txtEmail" class="required">Email</label> <asp:TextBox ID="txtEmail" ToolTip="Email" 
                    runat="server" MaxLength="150" />
           </div>
    
           <asp:PlaceHolder ID="phNewLabels" runat="server"></asp:PlaceHolder>
           <asp:Literal ID="litLabelIDs" runat="server" Visible="false" />
           <div>
                <label for="btnRegister"></label>
                <asp:Button ID="btnRegister" runat="server" Text="Register" />
           </div>
        </fieldset>
    </asp:Panel>
    Code:
    Private Sub CreateDynamicControls()
            phNewLabels.Controls.Clear()
    
            Dim oFunc As clsFunctions = New clsFunctions
            If txtAccessCode.Text.Trim.Length > 0 Then
                Dim ds As DataSet = oFunc.GetAddlFields(txtAccessCode.Text.Trim)
                Dim dv As DataView = New DataView(ds.Tables(0))
    
                For Each drv As DataRowView In dv
    
                    Dim iFieldID As Integer = drv("wpt_subscription_addl_field_id")
                    Dim sFieldLabel As String = drv("wpt_subscription_addl_field_label")
                    Dim iIsRequired As Integer = drv("wpt_subscription_addl_field_isreq")
                    litLabelIDs.Text &= iFieldID.ToString & ","
    
                    Dim sRequired As String = ""
                    Dim rv As RequiredFieldValidator = New RequiredFieldValidator
    
                    If iIsRequired = 1 Then
                        sRequired = "class=""required"""
    
                        rv.ID = "rfv" & iFieldID
                        rv.ControlToValidate = "txtTextBox" & iFieldID
                        rv.ErrorMessage = " Required!"
                        'rv.InitialValue = "*"
    
                    End If
    
                    Dim gcDivBlock As HtmlGenericControl = New HtmlGenericControl("div")
                    gcDivBlock.ID = "AddlField" & iFieldID
    
                    Dim litLiteral = New Literal
                    litLiteral.Text = String.Format("<label for=""{0}"" {2}>{1}</label>", "txtTextBox" & iFieldID, sFieldLabel, sRequired)
                    litLiteral.ID = "litLiteral" & iFieldID
    
                    Dim txtTextBox As TextBox = New TextBox
                    txtTextBox.Text = ""
                    txtTextBox.ID = "txtTextBox" & iFieldID
    
    
                    gcDivBlock.Controls.Add(litLiteral)
                    gcDivBlock.Controls.Add(txtTextBox)
                    If iIsRequired = 1 Then
                        gcDivBlock.Controls.Add(rv)
                    End If
                    phNewLabels.Controls.Add(gcDivBlock)
                Next
    
                If litLabelIDs.Text.EndsWith(",") Then
                    litLabelIDs.Text = litLabelIDs.Text.Substring(0, litLabelIDs.Text.Length - 1)
                End If
            End If
        End Sub
    
     Protected Sub btnACRegister_Click(sender As Object, e As System.EventArgs) Handles btnACRegister.Click
            If txtAccessCode.Text.Trim.Length > 0 Then
                CreateDynamicControls()
            End If
        End Sub
    
        Protected Sub btnRegister_Click(sender As Object, e As System.EventArgs) Handles btnRegister.Click
                Dim sIDs() As String = litLabelIDs.Text.Split(",")
                For Each iID As Integer In sIDs
                    Dim txtBox As TextBox = phNewLabels.FindControl("txtTextBox" & iID)
                    lblMsg.Text &= txtBox.Text & ","
                Next
        End Sub
    
        Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
            CreateDynamicControls()
        End Sub
    Last edited by lleemon; Jan 25th, 2014 at 09:23 AM.

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