web usercontrol question

Posted on 2012-08-24
Last Modified: 2012-09-05
I created a web usercontrol. The usercontrol has  3 imagebuttons.
How will I refer to the control's individual imagebutton- as to which was click.
What code use to assign funcitonality? thanks Give give example.
Question by:zachvaldez
    LVL 18

    Expert Comment

    by:Gary Davis
    There are a few ways to do this. One is to assign a different onclick event to each button. Another, shown here, is to have one onclick event that checks the ID of the button firing the event.

    Assuming this usercontrol (ASP.Net - C#):

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Controls_WebUserControl" %>
    <asp:ImageButton ID="ImageButton1" runat="server" 
        ImageUrl="~/images/buton1.gif" onclick="ImageButton_Click" />
    <asp:ImageButton ID="ImageButton2" runat="server" 
        ImageUrl="~/images/buton2.gif" onclick="ImageButton_Click"/>
    <asp:ImageButton ID="ImageButton3" runat="server" 
        ImageUrl="~/images/buton3.gif" onclick="ImageButton_Click"/>

    Open in new window

    Code behind:
    using System.Web.UI;
    using System.Web.UI.WebControls;
    public partial class Controls_WebUserControl : UserControl
        protected void ImageButton_Click(object sender, ImageClickEventArgs e)
            var button = (Button) sender;
            if (button.ID == ImageButton1.ID)
                // handle Button1
            else if (button.ID == ImageButton2.ID)
                // handle Button2
            else if (button.ID == ImageButton3.ID)
                // handle Button3

    Open in new window

    Gary Davis
    LVL 16

    Accepted Solution

    You can use CommandName attribute of the ImageButton as shown in code:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
        Inherits="WebUserControl" %>
    <asp:ImageButton ID="ImageButton1" runat="server" CommandName="First" ImageUrl="~/Img/diary.png"
        OnClick="ImageButton_Click" />&nbsp;
    <asp:ImageButton ID="ImageButton2" runat="server" CommandName="Second" ImageUrl="~/Img/files.png"
        OnClick="ImageButton_Click" />&nbsp;
    <asp:ImageButton ID="ImageButton3" runat="server" CommandName="Third" ImageUrl="~/Img/folder.png"
        OnClick="ImageButton_Click" />

    Open in new window

    And in code-behind file of user control you can handle command names as shown below:

     protected void ImageButton_Click(object sender, ImageClickEventArgs e)
            ImageButton button = sender as ImageButton;
            // Find label control in Default.aspx page in which user control has been placed
            Label info = this.NamingContainer.FindControl("lblMessage") as Label;
            switch (button.CommandName)
                case "First":
                    info.Text = "First button clicked";
                case "Second":
                    info.Text = "Second button clicked";
                case "Third":
                    info.Text = "Third button clicked";

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now