Solved

How to pick the image name of image in datalist.

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now