Adding server controls programmatically(Noob Question) missinghyperlink text

Discussion in 'ASP .Net' started by alun65@gmail.com, Jan 16, 2008.

  1. Guest

    I'm attempting to programmatically build up some HTML in the code
    behind. Like so:

    // Create Hyperlink
    HyperLink link = new HyperLink();
    link.NavigateUrl = "nice cat";
    link.Text = "Cats Sleeping";

    //Create Image
    Image img = new Image();
    img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jpg";

    //Add image nested in the hyperlink
    link.Controls.Add(img);

    //Add the link with it's controls to a placeholder
    PlaceHolder1.Controls.Add(link);

    When I run the code I'm expecting an picture of a cat with the text
    'Cats Sleeping' all enclosed in anchor tags. But instead all I get is
    the image but no text. What am I doing wrong?
     
    , Jan 16, 2008
    #1
    1. Advertising

  2. Re: Adding server controls programmatically(Noob Question) missing hyperlink text

    What's probably happening is the image is replacing the text since they
    technically want to fill the same spot. Instead of setting the text property
    of the link, try doing something like creating the text as a literal then
    adding the literal to the link's control collection.

    Literal myText = new Literal();
    myText.Text = "Cats Sleeping";
    link.Controls.Add(myText);


    --
    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - Expression

    <> wrote in message
    news:...
    > I'm attempting to programmatically build up some HTML in the code
    > behind. Like so:
    >
    > // Create Hyperlink
    > HyperLink link = new HyperLink();
    > link.NavigateUrl = "nice cat";
    > link.Text = "Cats Sleeping";
    >
    > //Create Image
    > Image img = new Image();
    > img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jpg";
    >
    > //Add image nested in the hyperlink
    > link.Controls.Add(img);
    >
    > //Add the link with it's controls to a placeholder
    > PlaceHolder1.Controls.Add(link);
    >
    > When I run the code I'm expecting an picture of a cat with the text
    > 'Cats Sleeping' all enclosed in anchor tags. But instead all I get is
    > the image but no text. What am I doing wrong?
     
    Mark Fitzpatrick, Jan 16, 2008
    #2
    1. Advertising

  3. Manish Guest

    Re: Adding server controls programmatically(Noob Question) missing

    Hi,

    You would need to make the Hyperlink control as the container to show both
    the text and image control in the Hyperlink control because this is the
    default behavior of the Hyperlink control to replace the text with the image.

    protected void Page_Load(object sender, EventArgs e)
    {
    // Create Hyperlink
    HyperLink link = new HyperLink();
    link.NavigateUrl = "nice cat";
    //link.Text = "Cats Sleeping";
    //link.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jpg";

    Label lb = new Label();
    lb.Text = "Cats Sleeping";

    link.Controls.Add(lb);
    //Create Image
    Image img = new Image();
    img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jpg";

    //Add image nested in the hyperlink
    link.Controls.Add(img);

    //Add the link with it's controls to a placeholder
    PlaceHolder1.Controls.Add(link);

    }

    Regards,
    Manish
    www.ComponentOne.com

    "Mark Fitzpatrick" wrote:

    > What's probably happening is the image is replacing the text since they
    > technically want to fill the same spot. Instead of setting the text property
    > of the link, try doing something like creating the text as a literal then
    > adding the literal to the link's control collection.
    >
    > Literal myText = new Literal();
    > myText.Text = "Cats Sleeping";
    > link.Controls.Add(myText);
    >
    >
    > --
    > Hope this helps,
    > Mark Fitzpatrick
    > Microsoft MVP - Expression
    >
    > <> wrote in message
    > news:...
    > > I'm attempting to programmatically build up some HTML in the code
    > > behind. Like so:
    > >
    > > // Create Hyperlink
    > > HyperLink link = new HyperLink();
    > > link.NavigateUrl = "nice cat";
    > > link.Text = "Cats Sleeping";
    > >
    > > //Create Image
    > > Image img = new Image();
    > > img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jpg";
    > >
    > > //Add image nested in the hyperlink
    > > link.Controls.Add(img);
    > >
    > > //Add the link with it's controls to a placeholder
    > > PlaceHolder1.Controls.Add(link);
    > >
    > > When I run the code I'm expecting an picture of a cat with the text
    > > 'Cats Sleeping' all enclosed in anchor tags. But instead all I get is
    > > the image but no text. What am I doing wrong?

    >
     
    Manish, Jan 16, 2008
    #3
  4. marss Guest

    On 16 Січ, 07:07, wrote:
    > I'm attempting to programmatically build up some HTML in the code
    > behind. Like so:
    >
    > // Create Hyperlink
    > HyperLink link = new HyperLink();
    > link.NavigateUrl = "nice cat";
    > link.Text = "Cats Sleeping";
    >
    > //Create Image
    > Image img = new Image();
    > img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jpg";
    >
    > //Add image nested in the hyperlink
    > link.Controls.Add(img);
    >
    > //Add the link with it's controls to a placeholder
    > PlaceHolder1.Controls.Add(link);
    >
    > When I run the code I'm expecting an picture of a cat with the text
    > 'Cats Sleeping' all enclosed in anchor tags. But instead all I get is
    > the image but no text.




    Try this:

    HyperLink link = new HyperLink();
    link.NavigateUrl = "nice cat";
    link.Text = string.Format("<img src='{0}'><br>{1}", "http://
    www.cats.org.uk/images/lhstop_home2.jpg", "Cats Sleeping");
    PlaceHolder1.Controls.Add(link);

    Regards,
    Mykola
    http://marss.co.ua - Casual ideas for web development
     
    marss, Jan 16, 2008
    #4
  5. Guest

    Re: Adding server controls programmatically(Noob Question) missing

    Thanks for both your great answers, that's excatly what's been
    happening. And thanks for the bit of code Manish I'll use it. Many
    thanks Again
     
    , Jan 16, 2008
    #5
    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. =?Utf-8?B?RCBTaGVsZG9u?=

    Adding validation controls programmatically

    =?Utf-8?B?RCBTaGVsZG9u?=, May 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,856
    =?Utf-8?B?RCBTaGVsZG9u?=
    May 11, 2004
  2. Tim Marsden

    Adding controls programmatically.

    Tim Marsden, May 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    591
    Steven Cheng[MSFT]
    May 25, 2004
  3. Guest
    Replies:
    4
    Views:
    7,233
    Steven Cheng[MSFT]
    Nov 9, 2005
  4. Chakravarti Mukesh

    Adding Controls Programmatically

    Chakravarti Mukesh, Jan 10, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    4,007
    brijesh patel
    Feb 14, 2013
  5. Replies:
    0
    Views:
    498
Loading...

Share This Page