Get ImageURL or ID of a photo when clicked

Posted on 2012-09-13
Last Modified: 2012-09-13
Hi  I already posted a question as to how I get the ImageUrl from an ImageButton and was advised to use CommandName.  However, I wanted to just have one method as follows:

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        string imageName = ImageButton1.CommandName;
        imageName += ".jpg";

This would work apart from the fact that each ImageButton has a different ID - i.e they are not all called ImageButton1!  Surely there is an easy way to do this apart from manually writing a different onClick method for each ImageButton??

Some of my code is as follows:

            <asp:ImageButton CommandName="image_1" ID="ImageButton1" ImageUrl="../images/image_1.jpg" runat="server"
                Width="200px" OnClick="ImageButton1_Click" />
            <asp:ImageButton CommandName="image_2" ID="ImageButton2" ImageUrl="../images/image_2.jpg" runat="server"
                Width="200px" OnClick="ImageButton1_Click" />
            <asp:ImageButton CommandName="image_3" ID="ImageButton3" ImageUrl="../images/image_3.jpg" runat="server"
                Width="200px" OnClick="ImageButton1_Click" />

Question by:Lorna70
    LVL 50

    Accepted Solution


    Author Comment

    Thanks Julian - you're a start - exactly what I was looking for (in your first link - didn't need to look any further :-))
    LVL 50

    Expert Comment

    by:Julian Hansen
    I enjoy being a start - thank you and you are most welcome.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 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

    18 Experts available now in Live!

    Get 1:1 Help Now