473,473 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Handling Events in Nested Controls

Hi,

I have nested User Controls like below.
User_Control_1
User_Control_11
User_Control_12(contains method DisplayMessage)

User_Control_2(contains event onButtonClick )

User_Control_1 and User_Control_2 are loaded in the PAGE_LOAD of a
Layout.aspx. This aspx page WIRES the event of the control
User_Control_2(event onButtonClick) to the method of
User_Control_12(DisplayMessage) as below

oControl2.onButtonClick = new
EventHandler(oControl1.oControl11.oControl12.Displ ayMessage);

The error returned is "Object reference not set to an instance of an
object."
Control12 is loeaded in Control11 and control11 is loaded in Control1.
Control1 is loaded in Layout.aspx. I do not want to use BubbleEvent.
Isn't there a simpler way of getting these controls connected. Below
is my code... Help appreciated

Below is the code of Layout.ascx.cs
public class Layout : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlTableCell tblcellControl1;
protected System.Web.UI.HtmlControls.HtmlTableCell tblcellControl2;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
NestedControls.Control1 oControl1 = (Control1)
LoadControl("Control1.ascx");
tblcellControl1.Controls.Add(oControl1);

NestedControls.Control2 oControl2 = (Control2)
LoadControl("Control2.ascx");
tblcellControl2.Controls.Add(oControl2);

oControl2.onButtonClick = new
EventHandler(oControl1.oControl11.oControl12.Displ ayMessage);
}
Below is the code for Control2

public abstract class Control2 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

public EventHandler onButtonClick
{
set{this.Button1.Click +=value; }
}

Below is the code for Control12(method to wire to)
public void DisplayMessage(object sender, EventArgs e)
{
Response.Write("Button Clicked of Control_12");
}

Please help me sort this problem.

Thank You.
Guru
Nov 17 '05 #1
1 2607
The problem I have had with dynamically adding controls (ie adding them
during Page_Load) is that the session state gets loaded before you have
created the controls and thus the event handlers and the controls are not
present until it is too late.

You can add the controls and event handlers in the OnInit function (don't
use the InitializeComponent function since that is a visual studio only
property and it will delete your code). Perferrably you would write your
control adding functions in their own function and call that function from
OnInit.

If you search for "lifecycle of asp.net" or something like that you can get
the particulars.

HTH,

Todd Thompson
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tim Geiges | last post by:
Since I am being challenged with learning c# I figured I could pass some of the pain on to you guys :-) I have another question(this one is important for me to fix before I can get my app to Beta)...
2
by: luca | last post by:
I'm trying to build a Server Control, it's a calendar to manage sellers appointments (don't answer me to use and custumize Calendar Control because unluckily it's not possible for this specific...
0
by: luca | last post by:
Hi all. My problem is that I can't handle events raised from child components within a composite server control when the control is created dynamically. Everything works fine if the same control...
12
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
2
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
7
by: Jay Douglas | last post by:
Greetings, I have a Windows form application that (naturally) instantiates all sorts of objects. I have a base object that contains an event. Lots of other objects inherit from this event. ...
6
by: Steve Hershoff | last post by:
Hi everyone, I've got a strange one here. There are two datagrids on my page, one nested within the other. I'll refer to them as the topmost and secondary datagrids. In the topmost...
1
by: Klaus Jensen | last post by:
Subject: Handling events of controls created at runtime I need to render some buttons at runtime based on a database, tie events to them, and handle the events when they fire. In Page_Load I...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.