Re: picture in data grid based on value

From: Steven Cheng[MSFT] (v-schang_at_online.microsoft.com)
Date: 07/07/04


Date: Wed, 07 Jul 2004 02:45:28 GMT

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



Relevant Pages