C# – Inheriting a web server control in a simple fashion

asp.netccustom-server-controls

I'm having a Repeater used as a sort of Paging TagCloud. To do so, I've added simple properties to the Page's ViewState such as a Page, RowCount, etc…

I feel like it doesn't belong there but I had bad experiences with server controls, debugging, dll and deployment.

Could I just inherit the Repeater class, add a few ControlState/ViewState properties and be able to use it exactly as a Repeater dragged straight from the ToolBox?

Here, having the following simple class:

    public class TagCloud : Repeater
{
    public int selectedIndex;

    public TagCloud()
    {
        selectedIndex = -1;
        //
        // TODO: Add constructor logic here
        //
    }

    public int SelectedIndex
    {
        get { return selectedIndex; }
        set { selectedIndex = value; }
    }
}

Without creating a new WebControlLibrary project, could this cs file stands in the App_Code folder and work like expected?

Thanks.

Best Answer

Yes, there is no problem doing this. As long as you use the control's property syntax:

public int RowCount
{

  get { return (int) (ViewState["RowCount"] ?? 0); }
  set { ViewState["RowCount"] = value; }

}

Also, to make your properties look the same as the default ones you can add Description or DefaultValue attributes.