【问题标题】:Create a control based on value in a DB根据数据库中的值创建控件
【发布时间】:2013-08-29 00:26:46
【问题描述】:

我需要在我的 ASP.NET 应用程序中创建一个动态构建的页面(使用 VB)。该页面收集报表的参数信息。

名称、存储过程和所需参数等报表信息在数据库表中。

在表格中,我有一个字段“control_type”,我希望可以将其用作我需要创建的控件类型,以收集所选报告的相关参数值。 因此,如果我选择 Report01,我会从 DB 中获取 Report01 的行,该报告行的参数数据告诉我,我需要将一个整数值收集到一个名为 i_company_id 的参数中,并创建控件以选择此值形式(在我动态创建的参数页面)显示“DropDownList”。

如果我从数据库中获取行并将该字段值放入变量中,那么我可以执行类似的操作

dim ddl as New VaraibleName

然后我可以正常填充列表,然后

panel.controls.add(ddl)

关键是能够创建一个控件,其中要创建的控件类型是变量或其他动态定义的方式。

【问题讨论】:

标签: asp.net vb.net


【解决方案1】:

我了解以下解决方案可以满足您的需求:

Private Function createVariable(inputName As String) As Object
    Dim outVariable As Object = New Object

    If (inputName = "DropDownList") Then
        Dim targetVariable As DropDownList = New DropDownList()
        With targetVariable
            'You might change here its properties (passed via Object array for example)
        End With

        outVariable = targetVariable
    End If
    'Add as many conditions as variables you are expecting

    Return outVariable
End Function

通过做来调用它:

Dim ddl As Object = createVariable("VariableName")
panel.Controls.Add(ddl)

如果您打算仅将此代码与Controls 一起使用,您可以将Object 替换为Control(无论哪种方式都可以)。

【讨论】:

  • 好主意!非常感谢。
猜你喜欢
  • 2019-12-13
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多