Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Dynamically displaying images in a gridview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gnewsgroup@gmail.com - 04 Sep 2007 21:54 GMT
I've googled and tried various approaches, but could not resolve this
problem.  The article at MSDN: Displaying Images in a GridView Column
only presents a simple case where all data (including the images) of
the gridview come from a single table/datasource.

Here is my situation. In my web application, I need to display
customer bills info in a gridview.

Customer names and contact info are from the Customer table.

If a customer's bill is past due, I display a red circle in the last
coumn of that row.
If a customer's bill has been paid, I display a green circle in the
last column of that row.
If a customer's bill isn't past due, I display a blue circle in the
last column of that row.

Simple, right?

These little colorful circles can be from a database table or from an
ArrayList.

I've tried a template field in the last column and then bind the
ImageUrl of the Image Control to a method I have in the code-behind,
like so:

<asp:Image ... ImageUrl='<%# GetIconPath() %>' ....></asp:Image>

It did not work.

I removed the Image control and simply tried:

<img src='GetIconPath()' />

And it still did not work.

Then, I tried using a Label control and in the code behind I say

lblImageURL.Text = "<img src='" + GetIconPath() + "'/>";

It still didn't work.

Then, I created another page GetIconPath.aspx, which simply outputs
the path of the icon as a string like "~/Images/red_cirlce.jpg", and
in the Image control, I say

<asp:Image .... ImageUrl="GetIconPath.aspx" ... ></asp:Image>

It still refuses to work.

I tried <asp:ImageField> instead of <asp:TemplateField>, and it still
refuses to work.  I've spent the whole day working on this, and cannot
resolve this problem.

Any hint is highly appreciated.
Mark Rae [MVP] - 04 Sep 2007 22:22 GMT
> Any hint is highly appreciated.

When something like this happens, my first thought is relative paths...

Are you *absolutely sure* that the path to the various image files is
correct...?

Bear in mind that the ImageUrl property of an <asp:Image> webcontrol is
expecting a relative or absolute URL pointing to a graphic e.g. jpg, bmp,
gif etc:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.image.imageur
l(vs.80).aspx


When you do a View Source and inspect the HTML, does that shed any light on
things...?

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

gnewsgroup@gmail.com - 05 Sep 2007 04:33 GMT
> <gnewsgr...@gmail.com> wrote in message
>
[quoted text clipped - 17 lines]
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

Well, path might be problem.  You reminded me that I did not use
ResolveUrl anywhere in the code.  May give it a try.  Thx.
John Mott - 04 Sep 2007 22:32 GMT
If it were me I'd do this in the onRowBound handler for the gridview -- that
would give you access to the data for that row. From that you could detect
the paid/late/not late status and the chance to directly drop the src into
an image tag.

john

> I've googled and tried various approaches, but could not resolve this
> problem.  The article at MSDN: Displaying Images in a GridView Column
[quoted text clipped - 51 lines]
>
> Any hint is highly appreciated.
gnewsgroup@gmail.com - 05 Sep 2007 04:38 GMT
> If it were me I'd do this in the onRowBound handler for the gridview -- that
> would give you access to the data for that row. From that you could detect
> the paid/late/not late status and the chance to directly drop the src into
> an image tag.
>
> john

I think this sounds like a solution.  I like to manually bind it too.
The problem is that I am sorta confused by the series of OnBinding,
OnBind, OnBound properties/methods.  The documentation at MSDN only
says raise the Bind/Binding/Bound event, without explaining (or I
missed the article) their differences. I am only guessing their
differences from their semantics.  So, is it the case that OnBinding
means at the time when the GridView is being bound to the DataSource,
and OnBound means immediately after the GridView has been bound to the
DataSource?  What is OnBind then?
Alexey Smirnov - 04 Sep 2007 22:51 GMT
On Sep 4, 10:54 pm, gnewsgr...@gmail.com wrote:
> I've googled and tried various approaches, but could not resolve this
> problem.  The article at MSDN: Displaying Images in a GridView Column
[quoted text clipped - 51 lines]
>
> Any hint is highly appreciated.

For example

<asp:TemplateColumn>
<ItemTemplate>
<%#"<img src='" + GetIconPath() + "'>"%>
</ItemTemplate>
</asp:TemplateColumn>
gnewsgroup@gmail.com - 05 Sep 2007 04:39 GMT
> On Sep 4, 10:54 pm, gnewsgr...@gmail.com wrote:
>
[quoted text clipped - 63 lines]
>
> - Show quoted text -

I think I did try this, and it didn't seem to work.  But since if
nothing else works, I'll have to give it shot again.  Thx.
gnewsgroup@gmail.com - 05 Sep 2007 04:50 GMT
On Sep 4, 11:39 pm, gnewsgr...@gmail.com wrote:

> > On Sep 4, 10:54 pm, gnewsgr...@gmail.com wrote:
>
[quoted text clipped - 68 lines]
>
> - Show quoted text -

Sentence.Remove("since");
Alexey Smirnov - 05 Sep 2007 08:19 GMT
On Sep 5, 5:39 am, gnewsgr...@gmail.com wrote:

> > On Sep 4, 10:54 pm, gnewsgr...@gmail.com wrote:
>
[quoted text clipped - 68 lines]
>
> - Show quoted text -

Mark told you to check the path

if you think you image is here

~/Images/red_cirlce.jpg

and your webform with the grid is here

~/webform.aspx
http://localhost/webform.aspx

Are you able to get the image as

http://localhost/Images/red_cirlce.jpg

?

When you said, that if a customer's bill is past due, it should be in
this color, if a customer's bill has been paid, then another color,
etc. you would need also to know a status of the bill, so I guess you
would need something like this

<asp:TemplateColumn>
<ItemTemplate>
<%#"<img src='" + GetIconPath(Eval("Status")) + "'>"%>
</ItemTemplate>
</asp:TemplateColumn>

...
protected string GetImage(object n)
{
if (n != DBNull.Value) {
if ((string)n == "paid") {
return ("/images/green_circle.jpg" );
}
...
else
return ("");
}

Another way is to name icon files in the same way as you named the
values of the "status" field. Say, you have status = 0 (past due), 1
(paid), 2 (isn't past due) then you can name your files as
circle_0.jpg, circle_1.jpg, circle_2.jpg and then you would not neet
any codebehind method

<asp:TemplateColumn>
<ItemTemplate>
<%#"<img src='/images/status_" + Eval("Status") + ".jpg'>"%>
</ItemTemplate>
</asp:TemplateColumn>
gnewsgroup@gmail.com - 06 Sep 2007 21:23 GMT
> On Sep 5, 5:39 am, gnewsgr...@gmail.com wrote:
>
[quoted text clipped - 124 lines]
>
> - Show quoted text -

Thank you for sharing.  I think the sample you gave at the end of your
post is a good solution.

I resolved the problem by using the ImageField of GridView (like that
shown in the MSDN article).  I created a view in the database that
includes the image path field, and then bind this data source to the
gridview.

Thanks go to all nice people who took the time to give hints.
Muhammad Naveed Yaseen - 05 Sep 2007 01:32 GMT
If "~/Images/red_cirlce.jpg" is assigned to client side <img> tag,
browser wont understand ~

If it is assigned to any Url type server control (as Image.ImageUrl),
server would translate ~ according to folder of page/user-control
containing this control, which may have relative path to images folder
different than the page being rendered on client-side.

Try using path relative to client page (with . or .. if needed). That
may give intellisense red-line on server code, but it would get
rendered properly on client-side.
gnewsgroup@gmail.com - 05 Sep 2007 04:41 GMT
> If "~/Images/red_cirlce.jpg" is assigned to client side <img> tag,
> browser wont understand ~
[quoted text clipped - 7 lines]
> may give intellisense red-line on server code, but it would get
> rendered properly on client-side.

Nope, VS2005 did not show me any curly red lines.  I am also
suspecting that it might be a path problem, as Mark has pointed out
also.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.