ASP.NET v2.0: Introducing BulletedList Control
page 2 of 5
by Colt Kwong
Feedback
Average Rating: 
Views (Total / Last 10 Days): 30570/ 58

ASP.NET 1.* Workaround

ASP.NET 1.* Workaround

In order to complete this kind of task (especially involved the retrieval and binding of data from data source), a couple of approaches that ASP.NET developers normally use nowadays:

  1. Simple Looping:
    Retrieve data from a data source and loop through each object in this data source by using something like a foreach operator, while developer can write out the data on screen / assign to a Label.Text in each iteration.

  2. Dynamic Control:
    Like approach 1, but create the HyperLink / Literal Control (with NavigateUrl) and inject into a PlaceHolder control, or build up a Table control dynamically.

  3. Repeater Control:
    Since Repeater is a basic templated data-bound list without any layout or styles, so developers can put a pair of <ul> and </ul> as the HeaderTemplate and FooterTemplate respectively and put a pair of <li> and </li> surrounding the returned value.

    E.g. (Source: ScottW's .Text)

    <asp:repeater id="Bloggers" runat="server">

          <HeaderTemplate>

                <ul>

          </HeaderTemplate>

          <ItemTemplate>

                <li>

                      <b><%# Content Here %></b>

                </li>

          </ItemTemplate>

          <FooterTemplate>

                </ul>

          </FooterTemplate>

    </asp:repeater>

  4. Custom Control (WebControl):
    Create a custom control and use a StringBuilder object with starting tag of <ul>, loop through the DataSource and append the data to that StringBuilder object, and finally append a closing tag of “</ul>”

    E.g. (Source: ASP.NET Developer's Cookbook)

          if (dataSource != null) {

                _html.Append("<ul>");

                foreach (object dataObject in dataSource) {

                      _html.Append("<li>");

                      _html.Append(dataObject.ToString());

                      _html.Append("</li>");

                }

                _html.Append("</ul>");

          }

  5. Custom Control (Repeater Control):
    Create a custom control inherited from Repeater control rather than inherited from Control/WebControl class as approach 4, and then modify the retrieved data as surrounded by a pair of <li> and </li> in the ItemDataBound event.

    E.g. (Source: Juliet's blog)

    public void InstantiateIn(Control container) {

         LiteralControl l;

         switch(templateType) {

             case ListItemType.Header:

                 l = new LiteralControl();

                 l.Text = "<ul";

                 if(CssClass != null && CssClass.Length > 0)

                     l.Text += " class=\"" + CssClass + "\"";

                 l.Text += ">";

                 container.Controls.Add(l);

                 break;

             case ListItemType.Item:

             case ListItemType.AlternatingItem:

                 l = new LiteralControl();

                 l.DataBinding += new EventHandler(Item_DataBinding);

                 container.Controls.Add(l);

                 break;

             case ListItemType.Footer:

                 l = new LiteralControl();

                 l.Text = "</ul>";

                 container.Controls.Add(l);

                 break;

         }

    }

     

    public void Item_DataBinding(object sender, EventArgs e) {

        LiteralControl l = (LiteralControl) sender;

        RepeaterItem container = (RepeaterItem) l.NamingContainer;

        l.Text = "<li>" + (string)(container.DataItem) + "</li>";

    }

View Entire Article

User Comments

Title: Direct Post   
Name: Icer
Date: 2007-03-20 5:17:40 AM
Comment:
It is pretty easy to redirect to another page: (The display style has to be "Hyperlink")

ListItem item = new ListItem(Title, Url);
BulletedList.Items.Add(item);

That´s it.
Title: Controlling URL in Hyperlink mode   
Name: Patrick Farrell
Date: 2007-02-26 12:36:37 PM
Comment:
In response to Russ's comments. Although it's not necessarily intuitive, when you are using the BulletedList and you have the display mode set to URL, ListItem.Value renders the link url, ListItem.Text renders the display.

http://patf.net/blogs
Title: Images do not exist   
Name: Ozma
Date: 2006-03-22 2:18:56 PM
Comment:
The images in this article are no longer at the URLs used in the markup
Title: Still Can't Post Directly to Another Page   
Name: Russ Brooks
Date: 2006-03-19 3:08:08 PM
Comment:
The control is essentially useless because we still can not post driectly to another page. Like most .NET controls, why isn't there an "Url" attribute and a way to append a query string? True we can use the onclick, capture the event in the code-behind, then redirect with an appended query string, but why cause two requests on the server and skew logging and traffic metrics? Even 5 years after the advent of .NET, Microsoft still thinks a website can be built with a single page.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-23 10:46:17 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search