Solved

making a composite control in asp.net 2.0

Posted on 2006-11-22
6
337 Views
Last Modified: 2012-05-05
Hi

I am using asp.net 2.0. I want to create a composite control that consists of a graphic next to a link button. The graphic and the link button are laid out in a table.

I was looking for some pointers on where to start. Specifically how do i make sure my composite control raises a click event which my main page can respond to. How do i get the control to render so that the button and graphic are laid out in a table.

thanks a lot

andrea
0
Comment
Question by:andieje
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:kraffay
ID: 17999954
vb or c#?
0
 

Author Comment

by:andieje
ID: 18002872
dont mind
0
 
LVL 9

Accepted Solution

by:
kraffay earned 500 total points
ID: 18021562
You have to build a two column table in a ascx file (Add New Item...Web User Control):

<%@ Control Language="C#" ClassName="EEControl" %>
<table>
    <tr>
        <td style="width: 100px">
            <asp:Image ID="Image1" runat="server" ImageUrl="http://www.experts-exchange.com/images/vipAccess.gif" /></td>
        <td style="width: 100px">
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton></td>
    </tr>
</table>

Now we need to wire up an event so that the page hosting the control know when the button was clicked.  Here's the code-behind:

public partial class EEControl : System.Web.UI.UserControl
{
    public event EventHandler Clicked;

    protected void OnClicked(EventArgs e)
    {
        if (Clicked != null)
        {
            Clicked(this, e);
        }
    }

   
    protected void Page_Load(object sender, EventArgs e)
    {
        LinkButton myLinkButton = new LinkButton();
        myLinkButton.Click += new EventHandler(this.LinkButton1_Click);

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        OnClicked(EventArgs.Empty);

    }

}

Now we can embed our control on any page and interact with it:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EEControlHost.aspx.cs" Inherits="EEControlHost" %>
<%@ Register Src="EEControl.ascx" TagName="EEControl" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:EEControl ID="EEControl" runat="server" OnClicked="EEControl_OnClicked"  />
    </div>
    </form>
</body>
</html>

Here's the code behind for our container page:

public partial class EEControlHost : System.Web.UI.Page
{

    protected void EEControl_OnClicked(object sender, EventArgs e)
    {

        Response.Write("You clicked the User Control");

    }
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:andieje
ID: 18076985
Hi

that's great - thanks very much

However, I would like to make this control implement the IButtonControl interface. How would i do that? I want the control to server as  a default button for the page

thanks
andrea
0
 
LVL 9

Expert Comment

by:kraffay
ID: 18077093
Try this:

public partial class EEControl : IButtonControl
0
 

Author Comment

by:andieje
ID: 18088329
but it doesnt implement any of the properties of the interface like validation group???
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

932 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

10 Experts available now in Live!

Get 1:1 Help Now