Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 19th, 2008, 12:50 PM
Registered User
 
Join Date: May 2008
Location: Kansas City, KS, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default User control question

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. Thanks in advance.


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" "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>
    <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" />
<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);
    }
}
  #2 (permalink)  
Old May 19th, 2008, 03:35 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This code is rather confusing:

1) default_click is in a place that doesn't permit the file to compile. I can only assume it is supposed to be part of the class "Default5".

2) You posted code for "blank.ascx.cs", but where is the ASCX file?

3) You needn't register the same user control file (blank.ascx) twice. You can reuse a single control in the page.

4) You have declared a delegate and event in both the "header" class and the "blank" class. This greatly confused the issue but I think I see why it is in there twice. I would guess that you added in the same delegate and event in blank in the hopes it would result in the automatic bubbling of the event. Unfortunately, it doesn't work that way.

In this line:
Code:
   header_area.Controls.Add(LoadControl(@"~/user_controls/header.ascx"));
you are creating a new instance of "header" which has the link buttons. You add this to the "header_area" div control. But then you wire the button click handler to the clicked events of your blank placeholder control instances (header, menu) instead of the actual header control. There is nothing in the "blank" class that ever calls those events.

A fix is to save off the instance of the header control you load so that you can wire up to its events:
Code:
   header myHeader;
   myHeader = (header)LoadControl(@"~/user_controls/header.ascx");
   header_area.Controls.Add(myHeader);
   if(Page.IsPostBack)
   {
      myHeader.clicked += new header.OnButtonClick(default_click);
   }
Incidentally, the <asp:panel> renders as a DIV tag. Alternatively, if all you really need is a control placeholder, try the <asp:placeholder> control which renders no HTML.

-Peter
compiledthoughts.com
  #3 (permalink)  
Old May 19th, 2008, 04:23 PM
Registered User
 
Join Date: May 2008
Location: Kansas City, KS, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are correct on the default_click. Sorry. I stripped out some code and should have seen that. Here is the remaining piece.

blank.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="blank.ascx.cs" Inherits="blank" %>
  #4 (permalink)  
Old May 19th, 2008, 04:38 PM
Registered User
 
Join Date: May 2008
Location: Kansas City, KS, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm going to integrate your info and should have some questions in a few minutes.

  #5 (permalink)  
Old May 19th, 2008, 04:59 PM
Registered User
 
Join Date: May 2008
Location: Kansas City, KS, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How do I add partial class c_menu into my project?

  #6 (permalink)  
Old May 20th, 2008, 08:36 AM
Registered User
 
Join Date: May 2008
Location: Kansas City, KS, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anybody?

  #7 (permalink)  
Old May 20th, 2008, 03:59 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Where did "c_menu" come from? Did you have success with my suggestion?

-Peter
compiledthoughts.com
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Help! Custom Server Control using User Control diehard ASP.NET 1.0 and 1.1 Professional 2 January 4th, 2006 12:33 PM
Help with control initialization in user control mike_remember ASP.NET 1.0 and 1.1 Professional 7 December 19th, 2005 11:08 AM
user control / code behind question whyulil General .NET 3 January 11th, 2005 11:17 AM
User Control stu9820 ASP.NET 1.0 and 1.1 Basics 20 July 20th, 2004 11:42 AM



All times are GMT -4. The time now is 06:47 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.