?
Solved

How to pick the image name of image in datalist.

Posted on 2012-03-30
5
Medium Priority
?
257 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 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