DataList controls

asp.net United Kingdom
  • 18 years ago

    I'm trying to create an ASP.NET page to show items from a SQL 7 database, and also hopefully items from an Exchange server, but when I try to format a datalist control I cant seem to data bind to it . Does anyone know of any walkthrus that show how to bind a data source to this control visually???


    Ta!!!

  • 18 years ago

    What do you mean by visually? Do you want me to give you a code example or point you to an article with loads of pictures? I don't know of one in any case.

  • 18 years ago

    Sorry! Didnt explain myself properly!!! The problem I seem to have is that I can do all the databinding to the datalist control quite easily, but I cant seem to bind any data to the items you add to the item templates to show the data . What I'm trying to do is to show a series of articles, with the titles in larger text and above the main body of text, and the datalist control seems ideal, but I cant make it show any data!!!


    Thanks

  • 18 years ago

    In a datalist or repeater control you create an templates for header, footer and items in the datalist. The example here creates a header template and a footer template and obviously starts a html table and adds rows for each item. The item template is applied for each row in the datasource assigned to the datalist in code. The dataitem specified - "name" must be a field name in the datasource. If my datasource had name, address, email for example I could add code for that too, ><%# databinder.eval(container.dataitem, "email") %>, for example.


    Code:

    <table>
    <aspataList ID="testDL" Runat="server">
    <HeaderTemplate>
    <tr><td><strong>My Header</strong></td></tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr><td><strong><%# databinder.eval(container.dataitem, "name") %></strong></td></tr>
    </ItemTemplate>
    </aspataList>
    </table>
    <asp:literal id="errormsg" runat="server" />


    Now in my code I need to create a datasource and my favorite way of doing that is with the datareader because it's nice and quick:-


    Code:

    dim conn as new sqlclient.sqlconnection(myConnString)
    dim cmd as new sqlclient.sqlcommand()
    dim dr as sqlclient.sqldatareader


    try
       conn.open
       cmd.connection=conn
       cmd.commandtype = CommandType.StoredProcedure
       cmd.commandtext = "up_MyStoredProced" 'which returns a name, address, email set of records for example
       dr = cmd.executereader
       testDL.datasource = dr
       testDL.databind
       dr.close
    catch ex as exception
       errormsg.text = ex.message
    finally
       conn.close
    end try



    Blinking 'eck even in code tags it create a green smiley - come on James

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger