Solved

How to pick the image name of image in datalist.

Posted on 2012-03-30
5
255 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Return array 3 34
Calculating percentage 2 48
Connect Gridview column to Textbox in C# 2 55
How does this modal work? 3 33
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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