views:

272

answers:

2

Hi,

Could someone try to recommend the best cause of action to solve the request below:

I have created a number of extended classes using the Repeater class as a Base and love the flexibility, the ease and the results of doing this. What I want to to do now is to make a similar Custom Web Control for a single object (i.e the DataSource not implementing a IListSource or IEnumerable). I have created the structure of what I'm trying to achieve by extending the repeater and then when setting the datasource using a list of 1 item to hold the object and then databinding.

e.g a rough example:

    Dim oObj as New MyObject(1)
    Dim gl As New Generic.List(of MyObiect)()
    gl.Add(oObj)
    rpt.DataSource = gl
    rpt.DataBind()

This does seem like a small hack around things, what I would like to be able to do is the following:

e.g new call, where my control is the new custom control:

    Dim oObj as New MyObject(1)
    myControl.DataSource = oObj 
    myControl.DataBind()

I want to able to define this custom control with various variables and properties, the result of which will enable the following type of layout:

<My:ObjControl ID="frm" runat="server">
    <Tabs>
        <My:Tab name="Details">
            <Items>
                <My:Item Type="Text" Label="First Name" Property="FirstName" />
                <My:Item Type="Text" Label="Last Name" Property="LastName" />
                <My:Item Type="Text" Label="Title" Property="Title" />
            </Items>
        </Tab>
        <My:Tab name="Address">
            <Items>
                <My:Item Type="Text" Label="Address 1" Property="Address1" />
                <My:Item Type="Text" Label="Address 2" Property="Address2" />
                <My:Item Type="Text" Label="Address 3" Property="Address3" />
            </Items>
        </Tab>
    </Tabs>
</My:ObjControl>

This implementation has to take place using WebForms, although it looks like using MVC would be the ideal approach considering the above. By doing this I want to create a flexible WebControl that uses reflection (that can be used for all classes that implement a specific interface) which will generate the required form with only the three lines of code behind (above) each time it's needed.

Should I just add a property to a custom repeater (DataObject) that takes an object and sets the DataSource accordingly and saving my time? Or is there a better way?

Hope this all makes sense!

Cheers, Steve

+3  A: 

You're trying way to hard to get around the fact that the repeater wants an List and you want to give it 1 instance. Just wrap it in a new List* oObj ) and move on. It's done all the time.

I certainly dunno what they heck is going on with that custom server control. Seems to me you are trying too hard again.

<asp:repeater id="whatever" runat=server>
   <ItemTemplate>First Name: <%# DataBinder.Eval(Container.DataItem,"FirstName") %></ItemTemplate>
</asp:repeater>
Chad Grant
Ok, it's very hard to get across the thought process but what my control will achieve is a produce a page with two li tabs 'Details' and 'Address' which can be tabbed between and on each it will have labels and matching form elements that can update the object. What you've shown has missed the point entirely - probably my fault in the wording of the question. Of course in your e.g I could just do: Eval("FirstName").
stibstibstib
Still overkill to do this much work for a couple of tabs and I still recommend a repeater. Yes I do not understand your question fully. I don't use Eval, DataBinder.Eval is the 1.0 way to do it and I see no reason to change yet, Habit.
Chad Grant
A: 

I completely understand your point of view, stibstibstib. I also wish the ASP.NET Framework came with a template driven server control like ListView but for a single data object. It seems unnecessary, and feels kinda hakish, to create a list out of a single item just to get it to work with Repeater and ListView. I get that it works. I get that it is a simple solution. But ASP.NET is a Framework, and it just seems like an omission that should be fixed. This article http://gadgetopia.com/post/5343 touches on the same concept, that there are two fundamental ways to view content on a web site: as a single item, and as an item in list. Still surprised ASP.NET doesn't have something like an ItemView server control

Bill