 |
| ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 27th, 2008, 11:30 AM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Putting an image in a Datalist, dependent on WHERE
I have a table of information that I want to display in a Gridview or a Datalist; when the boolean column "MadeinUSA" is TRUE, I want to display a little .gif flag image in that row of the displayed Gridview or Datalist, instead of just text or a checked checkbox.
With my shaky ASP.NET knowledge, I'm guessing I should do something with a template, inserting the flag image into the list or repeater's template with the Visible property set to "False", and then in the code-behind do something like "If MadeinUSA=1 Visible=TRUE".
So, I have a vague idea of how to do it, but I'm not sure exactly how to structure the syntax to make this work.
Any help would be appreciated!
|
|

May 28th, 2008, 07:03 AM
|
|
Authorized User
|
|
Join Date: Dec 2006
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I dont know, there might be an easy solution, but I have done this before in the following way...
I have used a GridView. In that I have added the column like this -
<asp:TemplateField HeaderText="IsActive">
<ItemTemplate>
<asp:Image runat="server" ImageUrl='<%#getIcon(Container.DataItem) %>'/>
</ItemTemplate>
</asp:TemplateField>
In the code behind I have added the following function -
protected string getIcon(object DataItem)
{
int status = Int32.Parse(DataBinder.Eval(DataItem, "status").ToString());
if (status == 0)
return "images/active.gif";
else
return "images/suspend.gif";
}
You can try out in this way. If anyone has any better solution please post that.
|
|

June 6th, 2008, 10:32 PM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
I will try this -- thank you very much!
|
|

June 7th, 2008, 04:21 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
If it's just a matter of hiding or showing the image, then you could also bind to the Visible property directly:
<asp:Image runat="server" ImageUrl="MadeinUSA.gif" Visible='<%# Bind("MadeinUSA") %>' />
For more complex scenarios (e.g. determining the correct image), angshujit's solution is perfect.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|
|

June 9th, 2008, 08:58 PM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Imar
Hi there,
If it's just a matter of hiding or showing the image, then you could also bind to the Visible property directly:
<asp:Image runat="server" ImageUrl="MadeinUSA.gif" Visible='<%# Bind("MadeinUSA") %>' />
For more complex scenarios (e.g. determining the correct image), angshujit's solution is perfect.
Cheers,
Imar
|
Perfect. This never occured to me, and yet is so simple.
I'm saving a copy of angshujit's method as well, for when I inevitably have to choose between multiple icons.
Thank you both very much!
|
|
 |