简体   繁体   中英

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. It's clear that GridView hasn't got a property like EditRowStyle-BackColor, but only EditRowStyle has. 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? Then it would work and be assignable via CssClass attribute in the markup. I would just add the properties to MyCustomGrid one by one, don't put them in another class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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