1

I'm not sure until I make a database call how many links I will need on a page and where the links point to. Thus, I need to create 'x' number of links within a div tag on the front end. Any ideas how this can be achieved?

Heres what I tried -

foreach (KeyValuePair<string, string> kvp in attachments)
{
    HyperLink hyp = new HyperLink();
    hyp.ID = kvp.Key;
    hyp.Text = kvp.Value;
    attachmentHiddenDiv.Controls.Add(hyp);
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
neuDev33
  • 1,573
  • 7
  • 41
  • 54

4 Answers4

2

Have a div in your aspx page with runat attribute value set to "server"

<div id="divLinks" runat="server"></div>

and in your code behind, you may loop thru the items and create as many links

StringBuilder str=new StringBuilder();

foreach (KeyValuePair<string, string> kvp in attachments))
{
  str.Append("<a href='../target.aspx?id="+kvp.Value+"'>kvp.Key</a>");  
}
divLinks.InnerHtml=str.ToString();
Shyju
  • 214,206
  • 104
  • 411
  • 497
2

There are a few different ways you can do this. The simplest would be adding a Literal control on the page and setting the .Text value in your code behind to the Html (list of links you manually build) you want displayed.

Another option would be using a Repeater and binding it to a list of url strings as the datasource, with a HyperLink item in the ItemTemplate which binds the NavigateUrl property. For example asp:HyperLink build NavigateUrl within Repeater using XPATH data

Community
  • 1
  • 1
mellodev
  • 1,597
  • 12
  • 20
1

Use a literal control. Place a literal control inside a div tag, generate the html from the DB values, and set the literal controls text property to the html string generated from the DB values.

Kenny Thompson
  • 1,494
  • 12
  • 28
0

You could use a repeater to iterate over a set of objects, creating markup for each object. The objects will be pulled from your database.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.aspx

Ian Newson
  • 7,679
  • 2
  • 47
  • 80