Handling Events in Nested Controls

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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
    DotNetGuru Guest

  2. Similar Questions and Discussions

    1. Handling Events Of DataGrid Template Item Controls - UNANSWERED
      If I place a checkbox control into a template column of a DataGrid, how do I gain access to the CheckChanged event handler procedure that...
    2. Events Handling Order
      Hi all, I'm a total newbie, so this might be stupid... Anyway, I've created an expanding tree of categories control, based on DataList. It works...
    3. (vb.net) Handling user-control events
      I created a web-project containg an .aspx file and a self- made User-Control (.acsx file) - all in vb.net. My aspx file contains a Submit button...
    4. Handling events in container controls?
      Hi, I have a sub in a user control that looks like this: Public Sub BatchDetail_ItemCommand(ByVal Sender As Object, ByVal e As...
    5. Handling events in a datagrid
      Hi, I am having a datagrid with 3 radiobuttons in one template column and a textbox in another template column. How can I disable or enable the...
  3. #2

    Default Re: Handling Events in Nested Controls

    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
    Todd Thompson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139