Question

I am adding multiple controls dynamically based on a dropdownlist that the user selects. i.e. if the user selects 3 then 3 sets of the controls are added. My problem is not in adding the controls, I can add them fine, I haven't added all my code but the main parts to understand what I am doing.

Once the controls have been created, the relevant info is captured. On the Update click I need to access the values of these dynamic controls by looping through in the correct order and retrieve the values and write to the database. I can't seem to access them correctly.

Hopefully I am making sense. Any help would be appreciated. Thanks

''Loop through first set of controls and get values and then the next set etc..

Dim Description as string = ''Get Textbox value
Dim Type as string = ''Get RadComboBox value
Dim XFieldName as string = ''Get RadComboBox value
Dim Colour as string = ''Get RadColorPicker value

Below is my Code:

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    RecreateControlsTxt("txt", "TextBox")
    RecreateControlsChart("comboChart", "RadComboBox")
    RecreateControls("combo", "RadComboBox")
    RecreateControlsCP("cp", "RadColorPicker")

End Sub

Protected Sub AddControls_Click(sender As Object, e As EventArgs) Handles AddControls.Click

    For i As Integer = 0 To ddlFieldNames.SelectedIndex
        CreateTextbox("txt-" & Convert.ToString(i + 1))
    Next

    For i As Integer = 0 To ddlFieldNames.SelectedIndex
        CreateComboChart("comboChart-" & Convert.ToString(i + 1))
    Next

    For i As Integer = 0 To ddlFieldNames.SelectedIndex
        CreateComboField("combo-" & Convert.ToString(i + 1))
    Next

    For i As Integer = 0 To ddlFieldNames.SelectedIndex
        CreateColourPicker("cp-" & Convert.ToString(i + 1))
    Next

End Sub

Private Sub CreateTextbox(ByVal ID As String)
    Dim txt As New TextBox()
    txt.ID = ID
    txt.Height = 20
    Me.divDesc.Controls.Add(txt)
End Sub

Private Sub CreateComboField(ByVal ID As String)
    Dim combo As New RadComboBox()
    combo.ID = ID
    combo.DataSource = Me.odsChartsSeriesField
    combo.DataTextField = "FieldNames"
    combo.DataValueField = "FieldNames"
    combo.DataBind()
    Me.divField.Controls.Add(combo)
End Sub

Private Sub CreateComboChart(ByVal ID As String)
    Dim comboChart As New RadComboBox()
    comboChart.ID = ID
    Dim item1 As New RadComboBoxItem()
    item1.Text = "Line"
    item1.Value = "smoothedLine"
    item1.ImageUrl = ("Images/linechart.png")
    comboChart.Items.Add(item1)
    Dim item2 As New RadComboBoxItem()
    item2.Text = "Column"
    item2.Value = "column"
    item2.ImageUrl = ("Images/bar chart.png")
    comboChart.Items.Add(item2)
    Dim item3 As New RadComboBoxItem()
    item3.Text = "Pie"
    item3.Value = "pie"
    item3.ImageUrl = ("Images/pie chart.jpg")
    comboChart.Items.Add(item3)
    Me.divChart.Controls.Add(comboChart)
End Sub

Private Sub CreateColourPicker(ByVal ID As String)
    Dim cp As New RadColorPicker()
    cp.ID = ID
    cp.ShowIcon = True
    cp.Style("padding-top") = "1px"
    cp.CssClass = "CustomHeight"
    Me.divCol.Controls.Add(cp)
End Sub

Protected Sub Update_Click(sender As Object, e As EventArgs) Handles Update.Click
    Try
            Dim alltxt = divDesc.Controls.OfType(Of TextBox)()
            Dim allcomboChart = divChart.Controls.OfType(Of RadComboBox)()
            Dim allcomboField = divField.Controls.OfType(Of RadComboBox)()
            Dim allcp = divCol.Controls.OfType(Of RadColorPicker)()

            ''Loop through first set of controls and get values and then the next etc..
            Dim Description as string = ''Get Textbox value
            Dim Type as string = ''Get RadComboBox value
            Dim XFieldName as string = ''Get RadComboBox value
            Dim Colour as string = ''Get RadColorPicker value

            If Page.IsValid Then
                Dim da As New dsSVTableAdapters.Chart
                Dim Result As String = da.Series(60, Description, Type, Colour, "YFieldName", XFieldName)
            End If

    Catch ex As Exception
        lblResult.Text = ex.Message
    End Try
End Sub
Was it helpful?

Solution

You have a repeated set of controls. Therefore you need a corresponding repeated set of variables that store these values. I suggest creating a class where you can store a variable (or property) set.

Public Class ControlSet
    Public Property Description As String
    Public Property Type As String
    Public Property XFieldName As String
    Public Property Colour As String
End Class

Create an array that holds these values

Dim Values = New ControlSet(ddlFieldNames.SelectedIndex) {}

And retrieve the values in a loop

For i As Integer = 0 To Values.Length - 1
    Values(i).Description = CType(divDesc.FindControl("txt-" & Convert.ToString(i + 1)), TextBox).Text
    Values(i).Type = CType(divChart.FindControl("comboChart-" & Convert.ToString(i + 1)), RadComboBox).SelectedValue
    Values(i).XFieldName = ...
    ...
Next

Also use the ID of the control; this helps to avoid confusion in case you have several controls of the same type.

OTHER TIPS

you can use .FindControl(string id) method, and you should keep the controls count in your view state or session:

Protected Sub Update_Click(sender As Object, e As EventArgs) Handles Update.Click
Try
        ''Loop through first set of controls and get values and then the next etc..
        For i As Integer = 0 To controlsCount - 1
            Dim Description as string = ((TextBox)divDesc.FindControl("txt-" & Convert.ToString(i + 1))).Text ''Get Textbox value
            Dim Type as string = ((RadComboBox)divChart.FindControl("comboChart-" & Convert.ToString(i + 1))).SelectedValue ''Get RadComboBox value
            Dim XFieldName as string = ((RadComboBox)divField.FindControl("combo-" & Convert.ToString(i + 1))).SelectedValue ''Get RadComboBox value
            Dim Colour as string = ((RadColorPicker)divField.FindControl("cp-" & Convert.ToString(i + 1))).SelectedValue ''Get RadColorPicker value

            If Page.IsValid Then
                Dim da As New dsSVTableAdapters.Chart
                Dim Result As String = da.Series(60, Description, Type, Colour, "YFieldName", XFieldName)
        Next
        End If

    Catch ex As Exception
        lblResult.Text = ex.Message
    End Try
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top