469,623 Members | 1,928 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

<a... <img..>> in a <td> from code-behind... this can't be that hard

Okay - I'm spinning my wheels on this one... can someone help me figure out
how to programmatically populate a table cell as follows (from C#
code-behind)? I've tried using a Literal control in the TableCell, a
HyperLink control, and an Image, but I'm not getting the results I want.

Here's the source of what I'm after (retrieved by viewing the source of a
page I'm trying to emulate):

<td><a
href="MyPage.aspx?SomeVar1=23&AnotherVar=9&Des=Thi s+is+the+text+description.
+"><img border=0 alt="Pick me" src="../images/myImage.gif"/><br>Here is
another description - under the image</a><br><br></td>

Basically the ultimate result I'm after is a two-column list of icons with a
bit of text underneath. Clicking the icon or associated text results in
going to the same destination. The table provides the layout of the
icons-with-text. Most variables (image paths, etc) get populated from a
database.

Any help would be GREATLY appreciated.
Nov 18 '05 #1
3 5206
Hi Henry,

You just need to understand how to build tables, rows, cells and other items
and add them to each other. It is like those Russian dolls where small parts
are contained inside bigger and bigger ones.

Here's some code using the brand new Visual Studio 2005 Express beta:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">

void Page_Load(object sender, EventArgs e)
{
Table tbl=new Table();
TableCell cell= new TableCell();
TableRow row=new TableRow();
HyperLink hlnk=new HyperLink();
hlnk.NavigateUrl="MyPage.aspx?SomeVar1=23&AnotherV ar=9&Des=This+is+the+text+description";
hlnk.ImageUrl="http://www.gc.ca/images/canada.gif";
cell.Controls.Add(hlnk);
hlnk=new HyperLink();
hlnk.NavigateUrl =
"MyPage.aspx?SomeVar1=23&AnotherVar=9&Des=This+is+ the+text+description";
hlnk.Text="<br>Here is another description - under the image";
cell.Controls.Add(hlnk);
row.Cells.Add(cell);
tbl.Rows.Add(row);
Page.Controls.Add(tbl);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

Ken
Microsoft MVP [ASP.NET]
"Henry Johnson" <ok**@dokey.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Okay - I'm spinning my wheels on this one... can someone help me figure
out
how to programmatically populate a table cell as follows (from C#
code-behind)? I've tried using a Literal control in the TableCell, a
HyperLink control, and an Image, but I'm not getting the results I want.

Here's the source of what I'm after (retrieved by viewing the source of a
page I'm trying to emulate):

<td><a
href="MyPage.aspx?SomeVar1=23&AnotherVar=9&Des=Thi s+is+the+text+description.
+"><img border=0 alt="Pick me" src="../images/myImage.gif"/><br>Here is
another description - under the image</a><br><br></td>

Basically the ultimate result I'm after is a two-column list of icons with
a
bit of text underneath. Clicking the icon or associated text results in
going to the same destination. The table provides the layout of the
icons-with-text. Most variables (image paths, etc) get populated from a
database.

Any help would be GREATLY appreciated.


Nov 18 '05 #2
I'm not a C#'er, so the syntax may be off, but this should work for you. Just set the attributes you need on each control.

TableCell td = new tablecell();
Hyperlink hyp = new hyperlink();
Image img = new Image();

hyp.controls.add(img);
hyp.controls.add(new LiteralControl("<br>Here is another description..."));
td.controls.add(hyp);
td.controls.add(new LiteralControl("<br><br>");

--Michael

"Henry Johnson" <ok**@dokey.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Okay - I'm spinning my wheels on this one... can someone help me figure out
how to programmatically populate a table cell as follows (from C#
code-behind)? I've tried using a Literal control in the TableCell, a
HyperLink control, and an Image, but I'm not getting the results I want.

Here's the source of what I'm after (retrieved by viewing the source of a
page I'm trying to emulate):

<td><a
href="MyPage.aspx?SomeVar1=23&AnotherVar=9&Des=Thi s+is+the+text+description.
+"><img border=0 alt="Pick me" src="../images/myImage.gif"/><br>Here is
another description - under the image</a><br><br></td>

Basically the ultimate result I'm after is a two-column list of icons with a
bit of text underneath. Clicking the icon or associated text results in
going to the same destination. The table provides the layout of the
icons-with-text. Most variables (image paths, etc) get populated from a
database.

Any help would be GREATLY appreciated.

Nov 18 '05 #3
Thank you Michael (and Ken Cox)... Two solutions that provide the desired
effect; primary difference being that Michael's nests the <img> within the
<a> tag in the rendered HTML (as I prefer in this instance), whereas Ken's
provides two <a> tags... each solution with its own merits.

Cheers!
"Raterus" <ra*****@spam.org> wrote in message
news:uN**************@TK2MSFTNGP10.phx.gbl...
I'm not a C#'er, so the syntax may be off, but this should work for you.
Just set the attributes you need on each control.

TableCell td = new tablecell();
Hyperlink hyp = new hyperlink();
Image img = new Image();

hyp.controls.add(img);
hyp.controls.add(new LiteralControl("<br>Here is another description..."));
td.controls.add(hyp);
td.controls.add(new LiteralControl("<br><br>");

--Michael

"Henry Johnson" <ok**@dokey.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Okay - I'm spinning my wheels on this one... can someone help me figure out how to programmatically populate a table cell as follows (from C#
code-behind)? I've tried using a Literal control in the TableCell, a
HyperLink control, and an Image, but I'm not getting the results I want.

Here's the source of what I'm after (retrieved by viewing the source of a
page I'm trying to emulate):

<td><a
href="MyPage.aspx?SomeVar1=23&AnotherVar=9&Des=Thi s+is+the+text+description. +"><img border=0 alt="Pick me" src="../images/myImage.gif"/><br>Here is
another description - under the image</a><br><br></td>

Basically the ultimate result I'm after is a two-column list of icons with a bit of text underneath. Clicking the icon or associated text results in
going to the same destination. The table provides the layout of the
icons-with-text. Most variables (image paths, etc) get populated from a
database.

Any help would be GREATLY appreciated.

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by El Diablo | last post: by
32 posts views Thread by Werner Partner | last post: by
1 post views Thread by prefersgolfing | last post: by
1 post views Thread by Santos L Halper | last post: by
3 posts views Thread by Manish Sawjiani | last post: by
6 posts views Thread by Summercool | last post: by
reply views Thread by =?Utf-8?B?R3JlZw==?= | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.