Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to pick the image name of image in datalist.

Posted on 2012-03-30
5
Medium Priority
?
258 Views
Last Modified: 2012-04-16
default.aspx
<asp:DataList ID="dtlist" runat="server" RepeatColumns="4" CellPadding="5">
<ItemTemplate>
<asp:Image Width="100" ID="Image1" ImageUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server" />
<br />
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server"/>
</ItemTemplate>
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
VerticalAlign="Bottom" />
</asp:DataList>
<asp:textbox id="t1" runat="server" Text=""/>

code behind
default.aspx.cs
1.How to make the image as a hyperlink.
2.How to pick the filename of the image on which the user clicks and store its name in textbox t1.


  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataList();
        }
    }
    protected void BindDataList()
    {
        DirectoryInfo dir = new DirectoryInfo(MapPath("Images"));
        FileInfo[] files = dir.GetFiles();
        ArrayList listItems = new ArrayList();
        foreach (FileInfo info in files)
        {
            listItems.Add(info);
        }
        dtlist.DataSource = listItems;
        dtlist.DataBind();

    }
0
Comment
Question by:searchsanjaysharma
[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
5 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 1500 total points
ID: 37790008
Modify your URLs to use below code:-

<asp:Image Width="100" ID="Image1" ImageUrl='<%# ((FileInfo)Container.DataItem).FullName %>' runat="server" />
<asp:HyperLink ID="HyperLink1" Text='<%# ((FileInfo)Container.DataItem).Name %>' NavigateUrl='<%# ((FileInfo)Container.DataItem).FullName %>' runat="server"/>

Here we are telling the Controls to use which property of DataItem(FileInfo) in your Container(ArrayList) to bind.

Also read this for handling click event in datalist:-
http://msdn.microsoft.com/en-us/library/df6hth3s.aspx
http://stackoverflow.com/questions/732553/asp-net-image-in-datalist-onclick-event-is-it-possible
0
 

Author Comment

by:searchsanjaysharma
ID: 37790615
After appending the code, the error is

: The type or namespace name 'FileInfo' could not be found (are you missing a using directive or an assembly reference?) <asp:Image Width="100" ID="Image1" ImageUrl='<%# ((FileInfo)Container.DataItem).FullName %>' runat="server" />


<ItemTemplate>

<asp:Image Width="100" ID="Image1" ImageUrl='<%# ((FileInfo)Container.DataItem).FullName %>' runat="server" />
<asp:HyperLink ID="HyperLink1" Text='<%# ((FileInfo)Container.DataItem).Name %>' NavigateUrl='<%# ((FileInfo)Container.DataItem).FullName %>' runat="server"/>
</ItemTemplate>
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
VerticalAlign="Bottom" />
</asp:DataList>
<asp:TextBox ID="t1" runat="server" Text=""></asp:TextBox>
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 37796469
You no need to have 2 controls image and hyperlink separately.  Instead you could use Hyperlink control with ImageURL. Please do refer the link

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlink.imageurl.aspx

Hence please do remove the image and specify the image url in the Hyperlink itself.
0
 

Author Comment

by:searchsanjaysharma
ID: 37850073
k
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37850074
ok
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

597 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