【问题标题】:add a style to System.Web.UI.Control向 System.Web.UI.Control 添加样式
【发布时间】:2012-07-07 03:07:32
【问题描述】:

我正在从数据库模式构建一大堆不同的控件。 当我在后面的代码中运行控件时,我想将控件和样式(作为数据库中的字符串......例如“颜色:白色;宽度: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))" 行我收到一个错误: “属性”不是“system.web.ui.control”的成员 我理解错误的含义,但有人知道解决这个问题的方法吗?

非常感谢, 斯科特

【问题讨论】:

    标签: asp.net vb.net attributes styles


    【解决方案1】:

    您应该使用WebControl 而不是ControlWebControl 派生自 Control,但包含 Attributes 属性。

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

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

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

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

    【讨论】:

    • 我还没有时间运行代码,但这确实解决了我在编译过程中遇到的错误......非常感谢老兄,非常感谢你的帮助!
    • @wotney 不用担心,请记住我们感谢投票和接受的答案作为回报;-)
    猜你喜欢
    • 2021-09-03
    • 2015-08-05
    • 2019-02-04
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多