views:

133

answers:

3

Hi, 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.

A: 

This works, and is one suggested way of building server controls. Try it and see.

John Saunders
I have a class TagCloud inheriting from Repeater having a simple int property. I must be missing attributes since the control doesn't seem to be exposed in the ToolBox. I would need the minimum required to do so.
Maxime
A: 

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.

Genady Sergeev
I know how to declare properties that use the ViewState, if I am missing attributes, it is probably at the class level since there is no "TagCloud" in my ToolBox.
Maxime
To have your custom control in the toolbox you need to create it in a new dll project, and then add it the toolbox via the "choose items" property.
Genady Sergeev
Ight, thanks +1, is there a way to be able to use it like this then:<whatever:TagCloud ID="TagCloud1" runat="server"></TagCloud>without having it in the ToolBox and using the default Repeater's designer.
Maxime
unfortunately no, if you want to use custom controls and use them in a markup way, you need to compile them to an assembly and then use the register directive. More information on that directive can be found http://msdn.microsoft.com/en-us/library/c76dd5k1.aspx here. If you don't want to deal with assemblies, you can create a repeater wrapper via user control, but this sounds little cumbersome to me.
Genady Sergeev
Thanks it makes sense.
Maxime
A: 

Inheriting from the ASP Repeater is a completely valid approach, so long as the control you are building IS a repeater with additional properties.

If you feel the control you need is actually "something else" that happens to have a repeater as part of its control set, then you likely need to make a composite control which adds a repeater to its control collection, along with whatever other controls are needed.

For example, you might want to have a control that has a repeater, a label of search results, links to go to the top and the bottom of the repeater's contents, etc. This composite control is not a repeater, but does use a repeater.

Jay S
Clarified my question.
Maxime