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);
}
4
  • @Oded Edited the question to add a sample code that I tried. Commented Apr 16, 2012 at 17:57
  • And what didn't work with that? What errors? Exceptions? Commented Apr 16, 2012 at 17:58
  • @Oded Silly mistake on my end. The above code works as well. Commented Apr 16, 2012 at 18:20
  • Perhaps you need to do some rubber duck problem solving... Commented Apr 16, 2012 at 18:21

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.