2

我正在从数据库模式构建一大堆不同的控件。当我在后面的代码中运行控件时,我想将控件和样式(作为数据库中的字符串......例如“颜色:白色;宽度:50 像素;高度:10 像素;”)传递给重新- 可用的功能。

这就是我认为我应该这样做的方式:

 Sub AddStylesToControl(ByRef ctrl As Control, Styles As String)

    'split styles string by semi colon
    Dim StyleArr() As String
    Dim count As Integer
    StyleArr = Styles.Split(";")
    For count = 0 To StyleArr.Length - 1

        '//ctrl.Attributes.Add("style", "color: red;")
        ctrl.Attributes.Add("style", StyleArr(count))
    Next

End Sub

不幸的是,在 "ctrl.Attributes.Add("style", StyleArr(count))" 这一行我收到一个错误:'attributes' is not a member of 'system.web.ui.control' 我理解错误的含义,但有人知道解决这个问题的方法吗?

非常感谢,斯科特

4

1 回答 1

7

您应该使用WebControl而不是Control. WebControl派生自Control但包括该Attributes属性。

此外,控件的“样式”属性应包含一个字符串,其中包含由 . 分隔的 CSS ;。因此,传递数据库中的整个字符串就足够了,您不需要再进行任何处理。

所以你的函数应该看起来像......

Sub AddStylesToControl(ByRef ctrl As WebControl, ByVal styles As String)
    ctrl.Attributes("style") = styles
End Sub

我已将其更改为直接设置(而不是Add),因为这将覆盖任何现有的"style". 如果集合中已经存在,则使用Attributes.Add将失败。"style"

于 2012-07-06T16:13:38.960 回答