Adding server controls programmatically(Noob Question) missing

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?
alun65 [ Mi, 16 Januar 2008 06:07 ] [ ID #1909216 ]

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

<alun65 [at] gmail.com> wrote in message
news:f0514a34-7bc4-405a-856d-adafbbed812c [at] v4g2000hsf.googleg roups.com...
> 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 [ Mi, 16 Januar 2008 06:30 ] [ ID #1909219 ]

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
>
> <alun65 [at] gmail.com> wrote in message
> news:f0514a34-7bc4-405a-856d-adafbbed812c [at] v4g2000hsf.googleg roups.com...
> > 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 [ Mi, 16 Januar 2008 06:41 ] [ ID #1909221 ]

Re: Adding server controls programmatically(Noob Question) missing

On 16 =D0=A1=D1=96=D1=87, 07:07, alu... [at] gmail.com wrote:
> I'm attempting to programmatically build up some HTML in the code
> behind. Like so:
>
> // Create Hyperlink
> HyperLink link =3D new HyperLink();
> link.NavigateUrl =3D "nice cat";
> link.Text =3D "Cats Sleeping";
>
> //Create Image
> Image img =3D new Image();
> img.ImageUrl =3D "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 =3D new HyperLink();
link.NavigateUrl =3D "nice cat";
link.Text =3D string.Format("<img src=3D'{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 [ Mi, 16 Januar 2008 06:58 ] [ ID #1909224 ]

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
alun65 [ Mi, 16 Januar 2008 06:59 ] [ ID #1909225 ]

dynamic links

Hi! How do I add links from a table? I do a query and want do make links to each row in the databasetable. The rows in the table contains text I want to show behind the links. So I need 'title' to be the text and 'primkey' to put in a Session and post it to next site:

private void getRecentUpdates()
{
Panel recentPanel = new Panel();
string recentSQL = "query";
DataTable recentData = SQLAccess.GetData(ecentSQL);
foreach (DataRow row recentData.Rows)
{
recentPanel.Controls.Add(createItems(row["Title"].ToString() ,row["PrimKey"].ToString()));
}
this.Controls.Add(recentPanel);
}

private Panel createItems(string title, string primKey)
{
Panel pnl = new Panel();
Session["recentInfo"] = primKey;
pnl.Controls.Add(new LiteralControl(title));
return pnl;
}

this method should work, I'm not shure about the Session this way, but I need to displays these as links and maybe use get to send primkey with the link to the next site instead. Please Help
Magnus[1] [ Mi, 09 April 2008 10:45 ] [ ID #1938595 ]
Microsoft » microsoft.public.dotnet.framework.aspnet » Adding server controls programmatically(Noob Question) missing

Vorheriges Thema: Connection to web server lost uploading files of 100mb
Nächstes Thema: Need to show a list of controls on the page