Solved

Modifying image path in DataList template

Posted on 2007-11-29
6
1,139 Views
Last Modified: 2008-02-01
Hi Experts !
I have a DataList that contains an Image as a template. The image contains the Images folder path... something like: Eval("Pictures","~/Images/{0}")
I'm going to use that DataList is a user control on different pages and I need to be able to change that image path to add sub folders to it: ex: ~/category1/Images{0} or ~/category2/Images{0}

I cannot find a way to access/change the ImageUrl property of the databound Image that's within the Datalist programatically. Does anyone know how to do it ?
Thank you.
<ItemTemplate>
        &nbsp;<asp:ImageButton ID="imgbImage" runat="server" Height="130px" ImageUrl='<%# Eval("Pictures","~/Images/{0}") %>'
            Width="130px" /><br />
        &nbsp;<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Pictures") %>'></asp:Label>
    </ItemTemplate>

Open in new window

0
Comment
Question by:techsuppoprt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:gocemi
ID: 20380431
Hi,
I suggeest taht you create public proerty of the control where user can specify the images path...

In the control inside

private string _imagesPath;
Piblic string ImagesPath
{
set{_imagesPath = value;}
}

And in the HTML
<dataListControl:CustomControl id=... runat=server ImagesPath="./PATHTOMYIMAGES" ... ></>

This way user will speciy from where datalist to fetch images

Hope this suits you
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 20383760
That's exactly the solution I was looking for.
Help me fix the HTML part please.. it doesn't work:

<asp:DataList ID="dtlAlbum" runat="server" BackColor="White" BorderColor="#336666"
    BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal"
    RepeatColumns="4">
    <DataListControl:CustomControl id="dtlAlbum" runat="server" ImagesPath="\Images"></>
    <FooterStyle BackColor="White" ForeColor="#333333" />
    <ItemStyle BackColor="White" ForeColor="#333333" />
    <SelectedItemStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
    <ItemTemplate>
        &nbsp;<asp:ImageButton ID="imgbImage" runat="server" Height="130px" ImageUrl='<%# Eval("Pictures","~/Images/{0}") %>'
            Width="130px" /><br />
        &nbsp;<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Pictures") %>'></asp:Label>
    </ItemTemplate>
</asp:DataList>

Open in new window

0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 20387324
Sorry, never mind the post above.
Doing public property is easy...Done.
The question is how do I make the DataList use that variable (ImagesPath ) as a source path to my Images folder?
Something like DataList.ItemTemplate.MyImage.ImageURL=ImagesPath;
What would be the correct way to do it ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:gocemi
ID: 20389421
You'll need to implemet itemDataBound event
And check if you'r datalist contains elements that are using imags
ImageButton, image ....
quick method is
for each call
go trough all controls in the Item that is being data bounded
and see if it is of type image or imagebutton
    than change the data that is bound.

//recusrsion function that will check all controls in the item of one datalist
private string checkData(Control c)
{
foreach(Control innerControl in c.controls)
{
    if(innerControl.Controls.length > 0)
   {
     checkData(innerControl);
   }
  else
  {
    //this is a single control
     if(c is image or c is imageButton)
   {
   ///here change the path to the image...
   (c as image).ImageUrl = [path to the images] + (c as image).imageURL
    }
  }
}
}


Note: I might have som sintax errors or maybe even code misspelling but the logic is clear :)

when items are databound on datalist, go trough every control that is contained in the item and if it is a button or image (or any other control that hold pictures) than change the path to the image

Hope this helps

Regards,



private void onitemdatabound(object sender, eventArgs)
{
checkData( (sender as datalistItem);
}
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 20504096
Sorry for the delay... I"ve been trying to figure it out :)
I'm still struggling with this. Especially with this part:

   ///here change the path to the image...
   (c as image).ImageUrl = [path to the images] + (c as image).imageURL

How do I circle through images in the folder ?
0
 
LVL 5

Accepted Solution

by:
gocemi earned 450 total points
ID: 20505765
You don't, i guess that image names will be sent from Database?
so just prefix the string with the property provided in the control
[path to the images] is public property that will be provided
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question