0

我根据用户选择的下拉列表动态添加多个控件。即,如果用户选择 3,则添加 3 组控件。我的问题不在于添加控件,我可以很好地添加它们,我没有添加所有代码,但主要部分是为了了解我在做什么。

创建控件后,将捕获相关信息。在更新单击上,我需要通过以正确的顺序循环访问这些动态控件的值并检索值并写入数据库。我似乎无法正确访问它们。

希望我说得通。任何帮助,将不胜感激。谢谢

''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

以下是我的代码:

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
4

2 回答 2

0

您有一组重复的控件。因此,您需要一组相应的重复变量来存储这些值。我建议创建一个可以存储变量(或属性)集的类。

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

创建一个包含这些值的数组

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

并在循环中检索值

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

还要使用控件的ID;如果您有多个相同类型的控件,这有助于避免混淆。

于 2012-12-18T21:59:25.320 回答
0

您可以使用.FindControl(string id)方法,并且应该将控件计数保持在视图状态或会话中:

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
于 2012-12-18T22:13:17.920 回答