Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Control Question

Status
Not open for further replies.

fadetoblack

Programmer
Jul 19, 2005
19
US
I'm trying to dynamically load user controls based on click events. But, for some reason
lb_websites in (listed below) header.ascx's click event doesn't register as being invoked.


Default5.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<%@ Register Src="~\user_controls\blank.ascx" TagName="header" TagPrefix="uc" %>
<%@ Register Src="~\user_controls\blank.ascx" TagName="menu" TagPrefix="uc" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns=" >
<head runat="server">
<title>Untitled Page</title>
<link href="cms.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="frame_form" runat="server">
<div id="header_area" class="header_area" runat="server"><uc:header ID="wuc_header" runat="server" /></div>
<div id="main_area" class="main_area" runat="server">
<div id="menu" class="menu" runat="server"><uc:menu ID="wuc_menu" runat="server" /></div>
</div>
</form>
</body>
</html>


Default5.aspx.cs
using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

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

protected void Page_Load(object sender, EventArgs e) {
header_area.Controls.Add(LoadControl(@"~/user_controls/header.ascx"));
if (Page.IsPostBack) {
this.wuc_header.clicked += new blank.OnButtonClick(default_click);
this.wuc_menu.clicked += new blank.OnButtonClick(default_click);
}
}
}

void default_click(string button_text) {
// loading controls
}


blank.ascx.cs
using System;

public partial class blank : System.Web.UI.UserControl {

// Delegate declaration
public delegate void OnButtonClick(string button_clicked);

// Event declaration
public event OnButtonClick clicked;
}


header.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/user_controls/header.ascx.cs" Inherits="header" %>
<asp:LinkButton ID="lb_websites" runat="server" Text="websites" OnClick="lb_Click" />&nbsp;
<asp:LinkButton ID="lb_administration" runat="server" Text="administration" OnClick="lb_Click" />


header.ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class header : System.Web.UI.UserControl {

// Delegate declaration
public delegate void OnButtonClick(string button_clicked);

// Event declaration
public event OnButtonClick clicked;

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

protected void lb_Click(object sender, EventArgs e) {
// Check if event is null
if (clicked != null) clicked(((LinkButton)sender).Text);
}
}
 
you need to register the event with each postback. i would also recommend removing them when the page is disposed.
Code:
override OnInt(EventArgs e)
{
   mycontrol.ClickEvent += ButtonClicked;
}

private void ButtonClicked(object sender, EventArgs e)
{
}

override void Dispose()
{
   mycontrol.ClickEvent -= ButtonClicked;
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I'm not sure how to integrate your code with mine. Can you show me using the header.ascx.cs code above?
 
Code:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class header : System.Web.UI.UserControl {

    // Delegate declaration
    public delegate void OnButtonClick(string button_clicked);

    // Event declaration
    public event OnButtonClick clicked;

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

    protected void lb_Click(object sender, EventArgs e) {
        // Check if event is null
        if (clicked != null) clicked(((LinkButton)sender).Text);
    }

override OnInt(EventArgs e)
{
   mycontrol.ClickEvent += ButtonClicked;
}

private void ButtonClicked(object sender, EventArgs e)
{
}

override void Dispose()
{
   mycontrol.ClickEvent -= ButtonClicked;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top