Link to home
Start Free TrialLog in
Avatar of zachvaldez
zachvaldezFlag for United States of America

asked on

web usercontrol question

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.
Avatar of Gary Davis
Gary Davis
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of Vikram Singh Saini
Vikram Singh Saini
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial