Solved

Modifying image path in DataList template

Posted on 2007-11-29
6
1,130 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
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:gocemi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 5

Expert Comment

by:gocemi
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now