Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Adding server controls programmatically(Noob Question) missinghyperlink text

Reply
Thread Tools

Adding server controls programmatically(Noob Question) missinghyperlink text

 
 
alun65@gmail.com
Guest
Posts: n/a
 
      01-16-2008
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?
 
Reply With Quote
 
 
 
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      01-16-2008
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

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?


 
Reply With Quote
 
 
 
 
Manish
Guest
Posts: n/a
 
      01-16-2008
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
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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?

>

 
Reply With Quote
 
marss
Guest
Posts: n/a
 
      01-16-2008
On 16 Січ, 07:07, (E-Mail Removed) 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
 
Reply With Quote
 
alun65@gmail.com
Guest
Posts: n/a
 
      01-16-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controls.Count, Controls.IsSynchronized, and Controls.SyncRoot Nathan Sokalski ASP .Net 4 09-05-2007 03:27 AM
basic: asp.net user controls: how to programmatically add attribute to all text box controls? ASP .Net 4 11-09-2005 02:14 AM
Difference in Web server Controls and HTML Server Controls =?Utf-8?B?c2lhag==?= ASP .Net 7 03-07-2005 07:15 PM
HTML Server Controls vs Web Server Controls brian ASP .Net Web Controls 1 01-24-2005 05:13 AM
Adding controls to Page.Controls collection Jeremy Ames ASP .Net 5 03-05-2004 08:33 PM



Advertisments