conditionally display icon in template column?

D

djc

I'm finishing up my first attempt at an asp.net app. I know this question is
likely documented all over the place but I have a handful of things to
figure out in order to finish this up and now I'm under a time constraint...
so... I'm digging into a couple of things myself and posting a couple of
questions as well in order to get this done.

I need to conditionally display an icon in a datagrid column. The icon is
not the only item in the column. The column needs to display the data from a
database field (string data) and then an icon after that data based on the
data in another field.

"data from field1" Icon1
"data from field1" Icon1
"data from field1" Icon2

The condition in psuedo is like so:
-------------------------------
If field2 = "Value1" Then
show Icon1
ElseIf field2 = "Value2" Then
show Icon2
Else
Don't show any icon
---------------------------------

how/where would I do something like this? I really do hate asking things
that I know I can/should just lookup myself (like in Scott Mitchells book on
the data controls) but right now I just need some quick answers.

any info would be greatly appreciated. Thanks.
 
E

Edge

djc said:
I'm finishing up my first attempt at an asp.net app. I know this
question is likely documented all over the place but I have a handful
of things to figure out in order to finish this up and now I'm under
a time constraint... so... I'm digging into a couple of things myself
and posting a couple of questions as well in order to get this done.

I need to conditionally display an icon in a datagrid column. The
icon is not the only item in the column. The column needs to display
the data from a database field (string data) and then an icon after
that data based on the data in another field.

"data from field1" Icon1
"data from field1" Icon1
"data from field1" Icon2

The condition in psuedo is like so:
-------------------------------
If field2 = "Value1" Then
show Icon1
ElseIf field2 = "Value2" Then
show Icon2
Else
Don't show any icon
---------------------------------

how/where would I do something like this? I really do hate asking
things that I know I can/should just lookup myself (like in Scott
Mitchells book on the data controls) but right now I just need some
quick answers.

any info would be greatly appreciated. Thanks.

Put your code in a function like this:

Function GetIconHTML(fieldValue As String) As String
If fieldValue="Value1" Then Return "<img src='images/icon1.gif'>"
If fieldValue="Value2" Then Return "<img src='images/icon2.gif'>"
Return ""
End Function

In your datagrid, use this code to create the icon:
<%# GetIconHTML(DataBinder.Eval(Container.DataItem,"Field2")) %>
 
E

Eliyahu Goldin

In code-behind, ItemDataBound event. Over there you can access row cells,
both for reading and writing.

Eliyahu
 
D

djc

Thank you.

Eliyahu Goldin said:
In code-behind, ItemDataBound event. Over there you can access row cells,
both for reading and writing.

Eliyahu

question from book
 
D

djc

I like that approach. Very cool. Thanks!

Edge said:
Put your code in a function like this:

Function GetIconHTML(fieldValue As String) As String
If fieldValue="Value1" Then Return "<img src='images/icon1.gif'>"
If fieldValue="Value2" Then Return "<img src='images/icon2.gif'>"
Return ""
End Function

In your datagrid, use this code to create the icon:
<%# GetIconHTML(DataBinder.Eval(Container.DataItem,"Field2")) %>
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top