picture in data grid based on value

B

Brian Henry

Hello, I am working on a simple email website that is kinda like hotmail...
but the listing of messages is in a data grid data bound to a table called
messages... now one of the columns in the messages data table is called read
if the value is 1 the message is read if 0 then unread... what i want to do
is show an image in the listing based on that value, if 1 then
icn-msg-read.gif will be showed in that column for that message row, if 0
then icn-msg-unread.gif needs to be shown, how would i go about doing this?
thanks!
 
K

Kevin Spencer

Dynamically create an image tag, or use an Image Control, and dynamically
set its URL.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
B

Brian Henry

yes, I know that but how would i do this in a data bound grid? i need to
pick which image will be displayed based off the value of the data column...
right now its just a numeric value read from a data bound column like this
<%#DataBinder.Eval(Container.DataItem,"Read")%> which ends up showing true
or false on the data grid because it's a bit value... i wanted to do
something like <img src='<%=
ImagePickFunction(DataBinder.Eval(Container.DataItem,"Read"))%> but that of
course does not work... how would i do this part of the problem? thanks
 
S

Steven Cheng[MSFT]

Hi Brian,

I think your approach is just correct and you've been very closer to the
solution. We can just do such task by using a custom helper functdion
together with the DataBinder.Eval function. But notice that we should
always put the databinding expression in the <%# %> tag rather than <%=
%> , they're different, below are some reference on these webform
expression syntax:

#Data Binding Expression Syntax
http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconDatabindingExpres
sionSyntax.asp?frame=true

#Web Forms Syntax
http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconASPPageSyntax.asp
?frame=true

As for your situation, first, we define a helper function in the page class:

protected string GetImageUrl(object readFlag)
{
int iRead = (int)readFlag;
if(iRead == 0)
{
return "Unread.gif";
}
else
{
return "Read.gif";
}
}

the helper function take an object param because the
DataBinder.Eval(container, expressioN) will return an object reference. And
since your datacolumn's value is a number value, we cast it to int and
return the proper image url depend on its value. After that, we can apply
it in the page's template together with the DataBinder.Eval expression,
just as below:

<img src='<%# GetImageUrl(DataBinder.Eval(Container.DataItem,"ReadColumn"))
%>'>

How do you think of this? If you have any other ideas, pleas also feel free
to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
B

Brian Henry

Hello,

That is what I was trying previously, doing it like this

<img src='<%# GetImageUrl(DataBinder.Eval(Container.DataItem,"ReadColumn"))
%>'>

but every time it did, it started poping up with the error with something
similar to object 'container' can not be found
 
B

Brian Henry

nevermind, it suddenly works with out changeing any code... odd behavior of
the code..
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top