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.
zachvaldezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Gary DavisDir Internet SvcsCommented:
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
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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";
                break;

            case "Second":
                info.Text = "Second button clicked";
                break;

            case "Third":
                info.Text = "Third button clicked";
                break;

            default:
                break;
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.