【问题标题】:Create 'Style' property on CompositeControl in ASP.Net在 ASP.Net 中的 CompositeControl 上创建“样式”属性
【发布时间】:2009-09-17 05:05:36
【问题描述】:

我在创建新的 CompositeControl 时遇到问题。

目前我有一个继承自 TextBox 的自定义控件,该控件没有定义 Style 属性,但我可以在页面标记中设置该属性并且样式将被正确应用。

我正在尝试创建一个具有与自定义控件相同功能的 CompositeControl,一切正常,直到我尝试在控件上设置 Style 属性,在编译时抛出此错误:"The 'Style ' 属性不能以声明方式设置”,即使我在 CompositeControl 的代码中声明了 'Style' 属性(即使我使用了 'new' 关键字),这种情况也会继续发生。

我不明白为什么我在其中一个控件中出现错误,而在另一个控件中没有。

我们将不胜感激。

谢谢

【问题讨论】:

  • 你能展示你的代码如何设置样式属性吗?

标签: asp.net custom-controls composite-controls


【解决方案1】:

我“解决”了它!

我只是不必在我正在创建的复合控件中定义 Style 属性,如果这样我可以将 Style 添加到控件并在控件的代码中访问它。

例如

  <cc1:MyCompositeControl runat="server" Style="background-color:Red" />

在 MyCompositeControl 代码中,我可以像这样访问它:

  foreach (string item in Style.Keys)
  {
     input.Style.Add(item, Style[item]);
  }

并将样式设置为我希望应用到的控件(在本例中为 input 控件)。

我仍然不明白为什么我不能设置 Style 属性,但好在我不需要。

感谢您的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多