简体   繁体   English

如何将自定义属性添加到自定义Web控件

[英]How to add custom properties to a custom webcontrol

I want to add custom typed properties to a webcontrol, like for example EditRowStyle in GridView, but in a way that the property's properties can be declared in Source view in ascx/aspx. 我想向Web控件中添加自定义类型的属性,例如GridView中的EditRowStyle,但是要以某种方式可以在ascx / aspx的“源”视图中声明该属性的属性。 It's clear that GridView hasn't got a property like EditRowStyle-BackColor, but only EditRowStyle has. 显然,GridView没有像EditRowStyle-BackColor这样的属性,而只有EditRowStyle。 Something like this: 像这样:

public class MyCustomGrid : GridView
{
  [...]
  private MyCustomSettings customSettings;
  public MyCustomSettings CustomSettings
        {
            get { return customSettings; }
        }
  [...]
}

public class MyCustomSettings 
{
  private string cssClass = "default";
  public string CssClass
  {
    get { return cssClass; }
    set { cssClass = value; }
  }
}

And the grid decalartion: 和网格十进制:

<c1:MyCustomGrid ID="grdCustom" runat="server" CustomSettings-CssClass="customcss" />

Because this solution doesn't work. 因为此解决方案不起作用。

public class MyCustomGrid : GridView
{
  [...]
  private MyCustomSettings customSettings;
  [PersistenceMode(PersistenceMode.InnerProperty),DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  public MyCustomSettings CustomSettings
        {
            get { return customSettings; }
        }
  [...]
}

[TypeConverter(typeof(MyCustomSettings))]
public class MyCustomSettings 
{
  private string cssClass = "default";
  public string CssClass
  {
    get { return cssClass; }
    set { cssClass = value; }
  }
}

Why can't you just have that CssClass property in MyCustomGrid? 为什么您不能仅在MyCustomGrid中拥有该CssClass属性? Then it would work and be assignable via CssClass attribute in the markup. 然后它将起作用,并且可以通过标记中的CssClass属性进行分配。 I would just add the properties to MyCustomGrid one by one, don't put them in another class. 我只是将这些属性一个接一个地添加到MyCustomGrid中,而不是将它们放在另一个类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM