?
Solved

creating a bulleted list dynamically

Posted on 2009-02-10
7
Medium Priority
?
466 Views
Last Modified: 2012-05-06
How do I create a bulleted list of items taken from a table in the database with a display mode in hyperlinks?
0
Comment
Question by:boneythomas
  • 4
  • 3
7 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 23598780
Do you know how to query your database or do you need code for that too  - whay kin dof database is it?
0
 
LVL 19

Expert Comment

by:daveamour
ID: 23598786
Sorry just noticved you posted in SQL 2005 so I assume thats the database!
Also can I assume this is an asp.net application?  If so which version of the framework are you using?
 
0
 

Author Comment

by:boneythomas
ID: 23598824
I am using 2.0 framework
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 19

Expert Comment

by:daveamour
ID: 23598861
Ok here is some code without the database access.
You would add the database code to populate the list of Links.  Let me know if you need an example of the database code too.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Bullets.aspx.cs" Inherits="Bullets" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <ul>
            <asp:Repeater ID="BulletLinks" runat="server">
                <ItemTemplate>
                    <li><a href=<%# DataBinder.Eval(Container.DataItem, "LinkUrl") %>><%# DataBinder.Eval(Container.DataItem, "LinkText") %></a></li>
                </ItemTemplate>
            </asp:Repeater>
        </ul>
    </form>
</body>
</html>
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Bullets : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BulletLinks.DataSource = GetLinks();
            BulletLinks.DataBind();
        }
    }
 
    private List<HyperLink> GetLinks()
    {
        List<HyperLink> links = new List<HyperLink>();
 
        //Database code goes here to populate links but we just do some manually
        //to show the idea
        HyperLink bbc = new HyperLink("BBC", "http://www.bbc.co.uk");
        HyperLink google = new HyperLink("Google", "http://www.google.co.uk");
        HyperLink ebay = new HyperLink("ebay", "http://www.ebay.co.uk");
        HyperLink msn = new HyperLink("msn", "http://www.msn.co.uk");
 
        links.Add(bbc);
        links.Add(google);
        links.Add(ebay);
        links.Add(msn);
 
        return links;
    }
}
 
public class HyperLink
{
    public HyperLink(string linkText, string linkUrl)
    {
        this.linkText = linkText;
        this.linkUrl = linkUrl;
    }
    
    private string linkText;
    private string linkUrl;
 
    public string LinkText
    {
        get
        {
            return linkText;
        }
 
        set
        {
            linkText = value;
        }
    }
 
    public string LinkUrl
    {
        get
        {
            return linkUrl;
        }
 
        set
        {
            linkUrl = value;
        }
    }
}

Open in new window

0
 

Author Comment

by:boneythomas
ID: 23598886
It would be good if you could help me out in explaining by using some database codes..thankyou
0
 
LVL 19

Accepted Solution

by:
daveamour earned 2000 total points
ID: 23598926
Ok attached is the updated C# code and below is the DDL for the table

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[HyperLinks]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[HyperLinks]
GO
CREATE TABLE [dbo].[HyperLinks] (
 [LinkID] [int] IDENTITY (1, 1) NOT NULL ,
 [LinkText] [varchar] (100) COLLATE Latin1_General_CI_AS NOT NULL ,
 [LinkUrl] [varchar] (100) COLLATE Latin1_General_CI_AS NOT NULL
) ON [PRIMARY]
GO
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
 
public partial class Bullets : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BulletLinks.DataSource = GetLinks();
            BulletLinks.DataBind();
        }
    }
 
    private List<HyperLink> GetLinks()
    {
        List<HyperLink> links = new List<HyperLink>();
 
        using (SqlConnection dbConnection = new SqlConnection("Data Source=ServerName;Initial Catalog=DatabaseName;User Id=Bob;Password=MyPassword;"))
        {
            using (SqlCommand dbCommand = new SqlCommand("Select LinkText, LinkUrl From HyperLinks"))
            {
                dbCommand.Connection = dbConnection;
 
                dbCommand.CommandType = CommandType.Text;
 
                dbConnection.Open();
 
                SqlDataReader reader = dbCommand.ExecuteReader();
 
                while (reader.Read())
                {
                    string linkText = reader.GetString(0);
                    string linkUrl = reader.GetString(1);
 
                    links.Add(new HyperLink(linkText, linkUrl));
                }
            }
        }
 
        return links;
    }
}
 
public class HyperLink
{
    public HyperLink(string linkText, string linkUrl)
    {
        this.linkText = linkText;
        this.linkUrl = linkUrl;
    }
    
    private string linkText;
    private string linkUrl;
 
    public string LinkText
    {
        get
        {
            return linkText;
        }
 
        set
        {
            linkText = value;
        }
    }
 
    public string LinkUrl
    {
        get
        {
            return linkUrl;
        }
 
        set
        {
            linkUrl = value;
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:boneythomas
ID: 31544927
Thankyou for the solution. This code should work for my application
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

862 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