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

  • Thread starter Thread starter Henry Johnson
  • Start date Start date
H

Henry Johnson

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=This+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.
 
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&AnotherVar=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]
 
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
 
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!


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
 
Back
Top