Link to home
Start Free TrialLog in
Avatar of techsuppoprt
techsuppoprt

asked on

Modifying image path in DataList template

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

Avatar of gocemi
gocemi
Flag of North Macedonia image

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
Avatar of techsuppoprt
techsuppoprt

ASKER

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

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 ?
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);
}
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 ?
ASKER CERTIFIED SOLUTION
Avatar of gocemi
gocemi
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial