I need to change a GridView image file path programmatically...

T

trint

I have this in my aspx file (using c#):
<asp:ImageButton ID="Imagebutton1" runat="server" alt='<%# Eval
( "name" ) %>' CommandName="select"
ImageUrl='<%#
Eval ( "image_file_name", "~/images/categories/{0}" ) %>' Width="50%" /
Sometimes the folder of the default image will be in /images/products
and not in /images/categories.
Is there a way to change this in my c# file under certain conditions?
Thanks,
Trint
 
D

djmc

ImageUrl='<%# DetectImagePath(Eval("image_file_name")) %>'

(I don't use eval, not sure if that's correct syntax)

then in code behind:

protected string DetectImagePath(object filename)
{
string sFileName = (string) filename;

if(File.Exists(String.Format("~/images/categories/{0}", sFileName))
return String.Format("~/images/categories/{0}", sFileName);
else if(File.Exists(String.Format("~/images/products/{0}", sFileName))
return String.Format("~/images/products/{0}", sFileName);
else
return "";
}
 
D

djmc

ImageUrl='<%# DetectImagePath(Eval("image_file_name")) %>'

(I don't use eval, not sure if that's correct syntax)

then in code behind:

protected string DetectImagePath(object filename)
{
string sFileName = (string) filename;

if(File.Exists(String.Format("~/images/categories/{0}", sFileName))
return String.Format("~/images/categories/{0}", sFileName);
else if(File.Exists(String.Format("~/images/products/{0}", sFileName))
return String.Format("~/images/products/{0}", sFileName);
else
return "";
}
 
T

trint

ImageUrl='<%# DetectImagePath(Eval("image_file_name")) %>'

(I don't use eval, not sure if that's correct syntax)

then in code behind:

protected string DetectImagePath(object filename)
{
string sFileName = (string) filename;

if(File.Exists(String.Format("~/images/categories/{0}", sFileName))
return String.Format("~/images/categories/{0}", sFileName);
else if(File.Exists(String.Format("~/images/products/{0}", sFileName))
return String.Format("~/images/products/{0}", sFileName);
else
return "";

}






- Show quoted text -

this works.
Thanks,
Trint
 

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

Top