How to display images in datagrid column header?

A

Alek Davis

Hi,

This must be simple, but I can't quite figure out how I can display images
in a header of a datagrid. For example, I need to display text followed by
the sort button (which is one of two images: ascending or descending)
followed by the help icon pointing to the help URL. Is this possible? Any
good methods of doing this? Thanks,

Alek
 
A

Alek Davis

Forgot to mention: I generate the image tag (with a hyperlink) dynamically
using a function in code behind. So I was looking to an option like this:

<ASP:TEMPLATECOLUMN
HeaderText='Info <%= GetHelpIconHtml("HelpTopicID") %>'>

The goal would be to produce HTML like this:

<TD...>Info <A Href=...><IMG Src=...></A></TD>

where <A Href.../A> is generated by the GetHelpIconHtml function in
code-behind.

Alek
 
C

Craig Deelsnyder

Forgot to mention: I generate the image tag (with a hyperlink)
dynamically
using a function in code behind. So I was looking to an option like this:

<ASP:TEMPLATECOLUMN
HeaderText='Info <%= GetHelpIconHtml("HelpTopicID") %>'>

The goal would be to produce HTML like this:

<TD...>Info <A Href=...><IMG Src=...></A></TD>

where <A Href.../A> is generated by the GetHelpIconHtml function in
code-behind.

Alek

As you see, the DataColumn has a HeaderText property:

http://msdn.microsoft.com/library/e...ontrolsDataGridColumnClassHeaderTextTopic.asp

it may be easier to do this in the code-behind than in the aspx binding
statement, even tho what you have looks OK as well. But you should be
able to use HTML like <img src=""...> in the HeaderText and it'll render
to an image when displayed. If you want to do it in the code-behind,
there is an ItemCreated event on the datagrid you can use in code-behind
and check e.Item.ItemType and if it's a header item type, you could set
the images then by adding controls to the e.Item.Cells. Or after binding,
you could go back and loop thru the cols and do it then (setting
col.HeaderText).

Here's a slightly different example, where the guy is setting his own
images in the header when sorting:

http://msdn.microsoft.com/library/e...ontrolsDataGridColumnClassHeaderTextTopic.asp
 
A

Alek Davis

OK, I figured it out. This can be done using <HEADERTEMPLATE> tags (right
before <ITEMTEMPLATE>). Inside of headertemplate, you can define any
controls, such as <ASP:LABEL>, etc., so I have three controls: one label for
header text label for help image URL, and one image for sorting. Wish
<HEADERTEMPLATE> were available via IntelliSense, it was just a lucky guess
(to use it) on my part.
 
A

Alek Davis

Thanks Craig,

I posted a message before seeing your reply. The problem with HeaderText is
that it translates whatever is defined inside of the quotes as a literal, so
there is not way (at least, I do not see it) to get the value of header text
or a part of it by calling a code-behind function (notice that I did not do
it in code-behind, I just want to call a common function). In my example,
the header text will produce 'Info <%= GetHelpIconHtml("HelpTopicID") %>'
instead of returning the string generated by GetHelpIconHtml appended to
"Info ") as I would've expected. Anyway, I figured out how to do it, and it
is really simple (see my previous post). Thanks for response.

Alek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top