Adding path info to an GridView asp:Image bound column

M

musosdev

Hi

I need to add the path to my images directory to the DataUrlField of a bound
ImageField in a GridView, but I can't work out how to do this.

Is there a a Path property I'm missing? Or can I append to each column as it
is inserted (by over-riding some sort of Populate method)?

Thanks,


Dan
 
M

Masudur

Hi

I need to add the path to my images directory to the DataUrlField of a bound
ImageField in a GridView, but I can't work out how to do this.

Is there a a Path property I'm missing? Or can I append to each column as it
is inserted (by over-riding some sort of Populate method)?

Thanks,

Dan

Hi,

Take a look at this article...

http://msdn2.microsoft.com/en-us/library/aa479350.aspx

do check out how ResolveUrl method is used.

Thanks
Masudur
 
S

Steven Cheng

Hi Dan,

Your question here is to how to add some custom path into the GridView
imagefield. Generally, you can use the "DataImageUrlField" and
"DataImageUrlFormatString" property to provide basic url and formatting
info. For example:

<asp:ImageField DataImageUrlField="title"
DataImageUrlFormatString="query.aspx?title={0}" ></asp:ImageField>

this can help you generate composite url path from the databound field
value. However, if you want further customization, I think you may consider
either of the following means:

1. You can use TemplateField and manuallyl put <asp:Image > control in it.
Thus, you can use inline databinding expression and inject the Image path
generation code logic into the databinding expression(you can define a
custom function in codebehind and call it).

=================
................
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("name") %>'></asp:Label>

<asp:Image ID="img" runat="server" ImageUrl='<%#
Your binding function or code logic here%>' />
</ItemTemplate>
</asp:TemplateField>
=========================


2. You can also use the RowDataBound event of Gridview. There, you can
access the databound fields and customize existing controls in the GridView
cell. e.g.

============
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
Trace.Write("GridView1_RowDataBound");
if (e.Row.RowType == DataControlRowType.DataRow)
{

DataRowView drv = (DataRowView)e.Row.DataItem;

long id = (long)drv[0];

Image img = e.Row.FindControl("img") as Image;
img.ImageUrl = ......;


}
}

============

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
 
M

musosdev

Thanks to both of you. I've sorted the problem with a TemplateField.

Cheers, Dan.



"Steven Cheng" said:
Hi Dan,

Your question here is to how to add some custom path into the GridView
imagefield. Generally, you can use the "DataImageUrlField" and
"DataImageUrlFormatString" property to provide basic url and formatting
info. For example:

<asp:ImageField DataImageUrlField="title"
DataImageUrlFormatString="query.aspx?title={0}" ></asp:ImageField>

this can help you generate composite url path from the databound field
value. However, if you want further customization, I think you may consider
either of the following means:

1. You can use TemplateField and manuallyl put <asp:Image > control in it.
Thus, you can use inline databinding expression and inject the Image path
generation code logic into the databinding expression(you can define a
custom function in codebehind and call it).

=================
................
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("name") %>'></asp:Label>

<asp:Image ID="img" runat="server" ImageUrl='<%#
Your binding function or code logic here%>' />
</ItemTemplate>
</asp:TemplateField>
=========================


2. You can also use the RowDataBound event of Gridview. There, you can
access the databound fields and customize existing controls in the GridView
cell. e.g.

============
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
Trace.Write("GridView1_RowDataBound");
if (e.Row.RowType == DataControlRowType.DataRow)
{

DataRowView drv = (DataRowView)e.Row.DataItem;

long id = (long)drv[0];

Image img = e.Row.FindControl("img") as Image;
img.ImageUrl = ......;


}
}

============

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
From: =?Utf-8?B?bXVzb3NkZXY=?= <[email protected]>
Subject: Adding path info to an GridView asp:Image bound column
Date: Tue, 25 Mar 2008 09:38:06 -0700
Hi

I need to add the path to my images directory to the DataUrlField of a bound
ImageField in a GridView, but I can't work out how to do this.

Is there a a Path property I'm missing? Or can I append to each column as it
is inserted (by over-riding some sort of Populate method)?

Thanks,


Dan
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top