Solved

Modifying image path in DataList template

Posted on 2007-11-29
6
1,135 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net web app 3 54
ComboBox to String Not Working 5 42
Load XML element 3 41
In the web.config conection string is there a way to tell it to use windows autertication. 2 32
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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