Add hyperlinks dynamically - newbie question

Discussion in 'ASP .Net Web Controls' started by JezB, May 24, 2004.

  1. JezB

    JezB Guest

    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 ?
     
    JezB, May 24, 2004
    #1
    1. Advertising

  2. JezB

    Raterus Guest

    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


    "JezB" <> wrote in message news:...
    > 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 ?
    >
    >
     
    Raterus, May 24, 2004
    #2
    1. Advertising

  3. JezB

    JezB Guest

    I forgot to say, my page is designed in Flow layout.

    "JezB" <> wrote in message
    news:...
    > 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 ?
    >
    >
     
    JezB, May 24, 2004
    #3
  4. 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. }


    "Raterus" <> wrote in message news:#...
    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


    "JezB" <> wrote in message news:...
    > 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 ?
    >
    >
     
    Robert Koritnik, May 25, 2004
    #4
  5. JezB

    JezB Guest

    Cheers mate
     
    JezB, May 25, 2004
    #5
  6. Thanks. I'll send you the bill for the beer I'll drink on this toast... ;)
    Before I get pissed... :)))))

    --
    RobertK
    { Clever? No just smart. }


    "JezB" <> wrote in message
    news:...
    > Cheers mate
    >
    >
    >
     
    Robert Koritnik, May 25, 2004
    #6
  7. JezB

    Raterus Guest

    Thanks, I'll have to remember that one. I knew there had to be an easier way!
    "Robert Koritnik" <> wrote in message news:...
    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. }


    "Raterus" <> wrote in message news:#...
    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


    "JezB" <> wrote in message news:...
    > 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 ?
    >
    >
     
    Raterus, May 25, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JezB
    Replies:
    6
    Views:
    3,329
    Raterus
    May 25, 2004
  2. Brett
    Replies:
    1
    Views:
    500
    Brett
    Sep 9, 2004
  3. Neil

    newbie question on hyperlinks

    Neil, Feb 19, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    521
  4. asha
    Replies:
    0
    Views:
    519
  5. Richard Jonker

    dynamically add hyperlinks and buttons to a datagrid

    Richard Jonker, Oct 2, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    743
    Niraj Ranka
    Oct 3, 2006
Loading...

Share This Page