Solved

How to pick the image name of image in datalist.

Posted on 2012-03-30
5
250 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

21 Experts available now in Live!

Get 1:1 Help Now