[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

web usercontrol question

Posted on 2012-08-24
2
Medium Priority
?
401 Views
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.
0
Comment
Question by:zachvaldez
2 Comments
 
LVL 18

Expert Comment

by:Gary Davis
ID: 38332561
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
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 1100 total points
ID: 38336584
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 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