0

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
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
user1475479
  • 103
  • 1
  • 14
  • Is this what you're looking for? `Description = alltxt.FirstOrDefault(Function(x) x.ID = "txt-0").Text` – Blachshma Dec 18 '12 at 21:41

2 Answers2

0

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.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Thanks @Olivier Jacot-Descombes, your method seems ideal. However when I step through the loop to access the controls values, I receive divDesc = {InnerText = (InnerText) threw an exception of type System.Web.HttpException.}" It's not getting the values for some reason, The divs are within a panel if that will makes any difference.Thank you – user1475479 Dec 19 '12 at 08:37
  • The value of a `TextBox` has to be accessed through the `Text` property. see [RadControls for Silverlight](http://www.telerik.com/help/silverlight/radcombobox-features-selection.html) for an explanation of how to access the value of the `RadComboBox` (I never used it). `InnerText` is used to access a Html literal. – Olivier Jacot-Descombes Dec 19 '12 at 14:34
  • I managed to get it working by accessing the control value with: `Dim vDescription As TextBox = DirectCast(divDesc.FindControl("txt-" & Convert.ToString(i + 1)), TextBox) Dim Description = vDescription.Text` thanks – user1475479 Dec 19 '12 at 20:15
  • Yes, `DirectCast` is better than `CType` in this case. – Olivier Jacot-Descombes Dec 20 '12 at 14:24
0

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
Ashkan
  • 3,322
  • 4
  • 36
  • 47