formatting repeater data in markup

Posted on 2007-10-10
Medium Priority
Last Modified: 2008-01-09
hi there,
hope someone can help here, im trying to use a repeater although Im not sure the correct way to format the data within the markup.  Simple example below.  My repeater that creates a hyperlink for each record I have from the database.  The repeater creates a list of links with the url address and the text link being the same.

                                <asp:Repeater ID="test" runat="server">
                                        <li><a href="<%# Container.DataItem %>" /><%# Container.DataItem %>
                                            </a> </li>

This is fine normally but I want to change the format of the data for the url address.

to create html like this

<li><a href="hello_there">hello there</a>
<li><a href="hello_again">hello again</a>
<li><a href="hello_today">hello today</a>
<li><a href="hello_to">hello to</a>

im not sure whether I should be formatting the data in the code behind file or within the markup file?  

Is it possible for the below:

  <li><a href="<%# Replace(" ","_",Container.DataItem) %>" /><%# Container.DataItem %>
</a> </li>

or can I format the dataitem in the code behind file?

Many thanks,

Question by:jimbona27
  • 7
  • 2
LVL 27

Accepted Solution

Sammy earned 480 total points
ID: 20054324
You can use the Repeater's ItemDataBound event to do the formating in run time
its well documented on MSDN

Good luck

Author Comment

ID: 20065329
im getting an error referring to this is not possible because of the repeater protection level?  any thoughts?  thanks

Author Comment

ID: 20065581
the error refers to the markup where I have added the onitembound event attribute within the repeater markup
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 27

Expert Comment

ID: 20065644
Can you post the code?
cs and aspx please

Author Comment

ID: 20093554
thanks, will do when I can..

Author Comment

ID: 20154901
ok backup now,
markup code:

                                <asp:Repeater ID="test" OnDataBinding="test_ItemDataBound" runat="server">
                                        <li><a href="/<%# Container.DataItem %>/" />
                                            <%# Container.DataItem %>

code behind:

    protected void test_ItemDataBound(object sender, DataListItemEventArgs e)


Author Comment

ID: 20154907
i get an error referring to the markup repeater line:
no overload for test_itemdatabound matches delegate system.eventhandler.

Author Comment

ID: 20156814
got it working now although im stuck with the following:

        if (e.Item.ItemType == ListItemType.Item)
         /// replace spaces with underscores..

using the following markup I get an error referring to the name does not exist...

<%# DataBinder.Eval(Container.DataItem, "name") %>

Author Comment

ID: 20161226
got around it,

is this an okay way of doing it or is the on data bind event a better approach?

        DataTable dt = new DataTable();
        DataRow dr;

        // Define the columns of the table.
        dt.Columns.Add(new DataColumn("name", typeof(String)));
        dt.Columns.Add(new DataColumn("url", typeof(String)));

        // execute query
        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
        conn.ConnectionString = @"...";
        OleDbDataReader reader = null;
        OleDbCommand cmd = new OleDbCommand("SELECT top 10 * FROM type", conn);
        reader = cmd.ExecuteReader();

        int counter = 0;
        string test= string.Empty;

        // populate datatable with DB info
        while (reader.Read())
            test= (string)reader["Name"];
            dr = dt.NewRow();
            dr[0] = test;
            dr[1] = test.Replace(" ", "-");

        // bind data
        DataList1.DataSource = dt;


 <asp:DataList ID="DataList1" runat="server">
                                        <li><a href="x/<%# DataBinder.Eval(Container.DataItem, "url") %>/" />
                                            <%# DataBinder.Eval(Container.DataItem, "name") %></a></li>

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question