DataList--is it wrong to do this? If so, what's the correct way?

Discussion in 'ASP .Net' started by Homer J. Simpson, Jul 31, 2007.

  1. I feel I'm not doing things correctly.

    I have a <asp:DataList> embedded in a <asp:panel> with its ScrollBars
    property set to Auto. Essentially, I want to format a bunch of records as a
    list in an area of the screen with a fixed size, so I want a scrollbar to
    appear if I have a lot of records. That works fine.

    Each row in my list consists of three fields that come from a database: a
    date, a title (both on the same line), and followed by a longer description
    below that first line. Knowing that the rendered HTML contains leading
    table, tr and td tags, I've added my own tags right into the <ItemTemplate>:

    <asp:DataList DataSourceID="..." >
    <ItemTemplate>
    <div class="clsDate"><%# Eval("MYDATE") %></div></td>
    <td><div class="clsTitle"><%# Eval( "MYTITLE") %></div></td>
    </tr>
    <tr>
    <td colspan="2"><div class="clsDescription"><%# Eval(
    "MYDESCRIPTION" ) %></div>
    </ItemTemplate>
    </asp:DataList>

    This lines up nicely:

    Date1 Title1
    Description1

    Date2 Title2
    Description2

    Date3 Title3
    Description3

    The rendered output is exactly what I want (and perfectly valid HTML)...but
    I can't help but get the feeling that I'm "cheating" by embedding my own
    partial tags--eg, my first <td> tag is a terminating </td>, knowing that the
    framework will already have generated a leading <td> by then, and I don't
    supply a terminating </td> either, knowing the framework will generate the
    final </td> tag...

    As far as I understand them, the DetailsView and FormView controls offer
    more layout flexibility, but are are intended for displaying a single record
    at a time.

    Is my approach a common and acceptable ASP.NET 2.0 practice? If it is, then
    so be and I'm worrying about nothing because "it works" and the rendered
    code is clean...but as much as possible, I'd like to do things the way
    you're supposed to and not hack partial tags into .aspx files (not to
    mention that it'll probably trip the WYSIWYG editor sooner or later)...

    I've seen more than a few references suggesting that the DataList has been
    mostly replaced by the GridView, but as far as I can tell, this isn't going
    to work in this case because I want the description to appear on its own row
    after the date and title fields (whereas the GridView is strictly
    line-oriented).

    Thoughts?
     
    Homer J. Simpson, Jul 31, 2007
    #1
    1. Advertising

  2. Homer J. Simpson

    Brandon Gano Guest

    Try using <asp:Repeater /> instead. The problem with making assumptions
    about the particular output of server controls is that the output could
    potentially change in a future version, breaking your code. For example, if
    Microsoft decided to use <ul /> to display menus instead of <table />.
    Microsoft? Please? While it is unlikely that the DataGrid output will ever
    change, I still think this applies as a bad practice.

    Here is an example (not tested):

    <table>
    <asp:Repeater ...>
    <ItemTemplate>
    <tr>
    <td class="clsDate">
    <%# Eval("MYDATE") %>
    </td>
    <td class="clsTitle">
    <%# Eval("MYTITLE") %>
    </td>
    </tr>
    <tr>
    <td class="clsDescription" colspan="2">
    <%# Eval("MYDESCRIPTION") %>
    </td>
    </tr>
    </ItemTemplate>
    </asp:Repeater>
    </table>

    This also allows you to assign classes at the <td /> level and avoid using
    redundant <div /> containers.


    "Homer J. Simpson" <root@127.0.0.1> wrote in message
    news:...
    >I feel I'm not doing things correctly.
    >
    > I have a <asp:DataList> embedded in a <asp:panel> with its ScrollBars
    > property set to Auto. Essentially, I want to format a bunch of records as
    > a list in an area of the screen with a fixed size, so I want a scrollbar
    > to appear if I have a lot of records. That works fine.
    >
    > Each row in my list consists of three fields that come from a database: a
    > date, a title (both on the same line), and followed by a longer
    > description below that first line. Knowing that the rendered HTML
    > contains leading table, tr and td tags, I've added my own tags right into
    > the <ItemTemplate>:
    >
    > <asp:DataList DataSourceID="..." >
    > <ItemTemplate>
    > <div class="clsDate"><%# Eval("MYDATE") %></div></td>
    > <td><div class="clsTitle"><%# Eval( "MYTITLE") %></div></td>
    > </tr>
    > <tr>
    > <td colspan="2"><div class="clsDescription"><%# Eval(
    > "MYDESCRIPTION" ) %></div>
    > </ItemTemplate>
    > </asp:DataList>
    >
    > This lines up nicely:
    >
    > Date1 Title1
    > Description1
    >
    > Date2 Title2
    > Description2
    >
    > Date3 Title3
    > Description3
    >
    > The rendered output is exactly what I want (and perfectly valid
    > HTML)...but I can't help but get the feeling that I'm "cheating" by
    > embedding my own partial tags--eg, my first <td> tag is a terminating
    > </td>, knowing that the framework will already have generated a leading
    > <td> by then, and I don't supply a terminating </td> either, knowing the
    > framework will generate the final </td> tag...
    >
    > As far as I understand them, the DetailsView and FormView controls offer
    > more layout flexibility, but are are intended for displaying a single
    > record at a time.
    >
    > Is my approach a common and acceptable ASP.NET 2.0 practice? If it is,
    > then so be and I'm worrying about nothing because "it works" and the
    > rendered code is clean...but as much as possible, I'd like to do things
    > the way you're supposed to and not hack partial tags into .aspx files (not
    > to mention that it'll probably trip the WYSIWYG editor sooner or later)...
    >
    > I've seen more than a few references suggesting that the DataList has been
    > mostly replaced by the GridView, but as far as I can tell, this isn't
    > going to work in this case because I want the description to appear on its
    > own row after the date and title fields (whereas the GridView is strictly
    > line-oriented).
    >
    > Thoughts?
    >
     
    Brandon Gano, Jul 31, 2007
    #2
    1. Advertising

  3. > Try using <asp:Repeater /> instead. The problem with making assumptions
    > about the particular output of server controls is that the output could
    > potentially change in a future version, breaking your code. For example,
    > if Microsoft decided to use <ul /> to display menus instead of <table />.
    > Microsoft? Please? While it is unlikely that the DataGrid output will ever
    > change, I still think this applies as a bad practice.


    This is *exactly* why I'm asking the question. Using knowledge of what
    should be a black box to hack your way around always leads to problems in
    the long term. Thanks for confirming this.

    > Here is an example (not tested):
    >
    > <table>
    > <asp:Repeater ...>
    > <ItemTemplate>
    > <tr>
    > <td class="clsDate">
    > <%# Eval("MYDATE") %>
    > </td>
    > <td class="clsTitle">
    > <%# Eval("MYTITLE") %>
    > </td>
    > </tr>
    > <tr>
    > <td class="clsDescription" colspan="2">
    > <%# Eval("MYDESCRIPTION") %>
    > </td>
    > </tr>
    > </ItemTemplate>
    > </asp:Repeater>
    > </table>
    >
    > This also allows you to assign classes at the <td /> level and avoid using
    > redundant <div /> containers.


    Yeah, the only reason I was using divs was to add the ability to assign a
    class for custom formatting--obviously I couldn't assign one to the first
    <td> tag, since I'm not the one providing it. As for the other cases
    (title, description), I only added the div (instead of using the enclosing
    td) for consistency. That only increased my suspicion that I was going
    about it the wrong way.

    <asp:Repeater> it is then--your example looks safe enough and very clean for
    my purposes. I hadn't looked into it (yet) as most of the references I've
    been using are concentrating on the gridview (being new to 2.0). I'm still
    at the stage where I'm discovering what's available and figuring out the
    best way to tackle common problems.
     
    Homer J. Simpson, Aug 1, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nevyn Twyll
    Replies:
    8
    Views:
    1,884
  2. joon
    Replies:
    1
    Views:
    516
    Roedy Green
    Jul 8, 2003
  3. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    445
  4. J.Ram
    Replies:
    7
    Views:
    655
  5. froil
    Replies:
    12
    Views:
    315
    Gunnar Hjalmarsson
    Mar 2, 2006
Loading...

Share This Page