Adding server controls programmatically(Noob Question) missinghyperlink text

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alun65@gmail.com

    #1

    Adding server controls programmatically(Noob Question) missinghyperlink text

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

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

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

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

    //Add the link with it's controls to a placeholder
    PlaceHolder1.Co ntrols.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

    #2
    Re: Adding server controls programmaticall y(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.A dd(myText);


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

    <alun65@gmail.c omwrote in message
    news:f0514a34-7bc4-405a-856d-adafbbed812c@v4 g2000hsf.google groups.com...
    I'm attempting to programmaticall y build up some HTML in the code
    behind. Like so:
    >
    // Create Hyperlink
    HyperLink link = new HyperLink();
    link.NavigateUr l = "nice cat";
    link.Text = "Cats Sleeping";
    >
    //Create Image
    Image img = new Image();
    img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";
    >
    //Add image nested in the hyperlink
    link.Controls.A dd(img);
    >
    //Add the link with it's controls to a placeholder
    PlaceHolder1.Co ntrols.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?

    Comment

    • =?Utf-8?B?TWFuaXNo?=

      #3
      Re: Adding server controls programmaticall y(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(objec t sender, EventArgs e)
      {
      // Create Hyperlink
      HyperLink link = new HyperLink();
      link.NavigateUr l = "nice cat";
      //link.Text = "Cats Sleeping";
      //link.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";

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

      link.Controls.A dd(lb);
      //Create Image
      Image img = new Image();
      img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";

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

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

      }

      Regards,
      Manish
      MESCIUS USA Inc. has JavaScript and .NET UI controls, datagrids, reporting solutions, spreadsheets, document APIs and more to take your applications further.


      "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.A dd(myText);
      >
      >
      --
      Hope this helps,
      Mark Fitzpatrick
      Microsoft MVP - Expression
      >
      <alun65@gmail.c omwrote in message
      news:f0514a34-7bc4-405a-856d-adafbbed812c@v4 g2000hsf.google groups.com...
      I'm attempting to programmaticall y build up some HTML in the code
      behind. Like so:

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

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

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

      //Add the link with it's controls to a placeholder
      PlaceHolder1.Co ntrols.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?
      >

      Comment

      • marss

        #4
        Re: Adding server controls programmaticall y(Noob Question) missinghyperlin k text

        On 16 Січ, 07:07, alu...@gmail.co m wrote:
        I'm attempting to programmaticall y build up some HTML in the code
        behind. Like so:
        >
        // Create Hyperlink
        HyperLink link = new HyperLink();
        link.NavigateUr l = "nice cat";
        link.Text = "Cats Sleeping";
        >
        //Create Image
        Image img = new Image();
        img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";
        >
        //Add image nested in the hyperlink
        link.Controls.A dd(img);
        >
        //Add the link with it's controls to a placeholder
        PlaceHolder1.Co ntrols.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.NavigateUr l = "nice cat";
        link.Text = string.Format(" <img src='{0}'><br>{ 1}", "http://
        www.cats.org.uk/images/lhstop_home2.jp g", "Cats Sleeping");
        PlaceHolder1.Co ntrols.Add(link );

        Regards,
        Mykola
        http://marss.co.ua - Casual ideas for web development

        Comment

        • alun65@gmail.com

          #5
          Re: Adding server controls programmaticall y(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

          Comment

          Working...