Solved

making a composite control in asp.net 2.0

Posted on 2006-11-22
6
338 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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