Add hyperlinks dynamically - newbie question

J

JezB

I have a loop in which I'm adding hyperlinks to a page dynamically :-

foreach (string x in ...)
{
HyperLink h = new HyperLink();
h.Text = x;
h.NavigateUrl = x+".aspx";
this.Controls.Add(h);
}

This works fine but the controls are added one after each other in one long
line. How do I force each one to be displayed on a new line ?
 
R

Raterus

Well, how would you do it in html? use a <br> tag? If you want to that, you'll need to probably use a HtmlGenericControl and set the tagname property. Something like this

foreach (string x in ...)
{
HyperLink h = new HyperLink();
HtmlGenericControl hgc = new HtmlGenericControl();
hgc.tagname="br";
h.Text = x;
h.NavigateUrl = x+".aspx";
this.Controls.Add(h);
this.controls.add(hgc);
}

This is one of I'm sure a ton of ways to change the rendered output. You could also try adding the hyperlink data to an array, and then binding it to a repeater. Lots of options really,

--Michael
 
R

Robert Koritnik

Insted of this control you can make it even easier (short code):

foreach(string x in ...)
{
HyperLink h = new HyperLink();
h.Text = x;
h.NavigateUrl = x + ".aspx";
this.Controls.Add(h);
this.Controls.Add(new LiteralControl("<br>"));
}

Don't forget to include:
uses System.Web.UI

Or instead you can also change tle last line of code (part) to:
....new System.Web.UI.LiteralControl("<br>")...

--
RobertK
{ Clever? No just smart. }


Well, how would you do it in html? use a <br> tag? If you want to that, you'll need to probably use a HtmlGenericControl and set the tagname property. Something like this

foreach (string x in ...)
{
HyperLink h = new HyperLink();
HtmlGenericControl hgc = new HtmlGenericControl();
hgc.tagname="br";
h.Text = x;
h.NavigateUrl = x+".aspx";
this.Controls.Add(h);
this.controls.add(hgc);
}

This is one of I'm sure a ton of ways to change the rendered output. You could also try adding the hyperlink data to an array, and then binding it to a repeater. Lots of options really,

--Michael
 
R

Robert Koritnik

Thanks. I'll send you the bill for the beer I'll drink on this toast... ;)
Before I get pissed... :)))))
 
R

Raterus

Thanks, I'll have to remember that one. I knew there had to be an easier way!
Insted of this control you can make it even easier (short code):

foreach(string x in ...)
{
HyperLink h = new HyperLink();
h.Text = x;
h.NavigateUrl = x + ".aspx";
this.Controls.Add(h);
this.Controls.Add(new LiteralControl("<br>"));
}

Don't forget to include:
uses System.Web.UI

Or instead you can also change tle last line of code (part) to:
...new System.Web.UI.LiteralControl("<br>")...

--
RobertK
{ Clever? No just smart. }


Well, how would you do it in html? use a <br> tag? If you want to that, you'll need to probably use a HtmlGenericControl and set the tagname property. Something like this

foreach (string x in ...)
{
HyperLink h = new HyperLink();
HtmlGenericControl hgc = new HtmlGenericControl();
hgc.tagname="br";
h.Text = x;
h.NavigateUrl = x+".aspx";
this.Controls.Add(h);
this.controls.add(hgc);
}

This is one of I'm sure a ton of ways to change the rendered output. You could also try adding the hyperlink data to an array, and then binding it to a repeater. Lots of options really,

--Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top