Solved

making a composite control in asp.net 2.0

Posted on 2006-11-22
6
336 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
Comment Utility
vb or c#?
0
 

Author Comment

by:andieje
Comment Utility
dont mind
0
 
LVL 9

Accepted Solution

by:
kraffay earned 500 total points
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:andieje
Comment Utility
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
Comment Utility
Try this:

public partial class EEControl : IButtonControl
0
 

Author Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 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