Hi, All
Before... my English is not so good so forgive me.
I have a problem with the use of different usercontrols, and there events.
I have made a simple script with this problem because the original script is simple to big to place in this Question Box.
The following files I am using for this sample script:
// the three usercontrols
control1.ascx
// containing: a button with the name: Text="Button on control 1?
control2.ascx
// containing: a button with the name: Text="Button on control 2?
control3.ascx
// containing: a button with the name: Text="Button on control 3?
// the three code behind classes
control1.cs
control2.cs
control3.cs
// the main class
simpleclass.cs
// and the default start page
default.aspx
=====================================================
I have used an enumeration to give the different usercontrols a unique name.
public enum ControlName { Control1, Control2, Control3 };
I have made a delegate to define an event
public delegate void ControlChangeEventHandler(ControlName c);
The job for this event is simply: change the current usercontrol to another one
I have created an interface
public interface IControls
{
event ControlChangeEventHandler OnControlChange;
void SetControl(ControlName c);
}
Look at the script below to see what the SetControl does.
**************************************************
After compiling and loading the default.aspx you see a button, which belongs to the first usercontrols
this button has the text: ?Button on control 1?.
I have made a response.write : HttpContext.Current.Response.Write("<script language='javascript'>alert('" + str + "');</script>"); which is able to start an alert box on the client, this is only placed in the different events to see which is firing and which not.
When I click on this button the following steps are executed:
? protected override void OnInit(EventArgs e)
? protected override void CreateChildControls()
? private void Page_Load(object obj, EventArgs e)
? and finally: private void Control1_button_Click(object sender, EventArgs e)
? So far it works. in Control1_button_Click the event this.OnControlChange(ControlName.Control2); is executed, the purpose is that this event removes the ControlName.Control1 and replace this with ControlName.Control2.
? Now the button from the second usercontrol (control2.ascx) is loaded in the page.
? Next step: pressing on this second button the events OnInit, CreateChildControls, Page_Load are fired but NOT the click handler : private void Control2_button_Click(object sender, EventArgs e)
? in this handler show below in the script, the ?this.OnControlChange(ControlName.Control3);? must be executed, to place the third button on the page. But the Control2_Button_Click is never reached, so I get not the third button on the page but?  the first one. Maybe is there a simple solution, but after several week searching on the internet, and put many ours in it, without results.
I want by pressing the first button from the control1.ascx, see the second button from the usercontrol control2.ascx and by pressing this one I wanted to see the third button from usercontrol3.ascx.
I hope that there is an expert who can help me with this problem.
**************************************************
=========================================================
The scripts: simpleclass.cs
=========================================================
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public delegate void ControlChangeEventHandler(ControlName c);
public enum ControlName { Control1, Control2, Control3 };
public interface IControls
{
event ControlChangeEventHandler OnControlChange;
void SetControl(ControlName c);
}
namespace testplace
{
public class simpleclass : Page, IControls
{
protected PlaceHolder placeholder1;
public event ControlChangeEventHandler OnControlChange;
private Control control1;
private Control control2;
private Control control3;
public void SetControl(ControlName c)
{
for (int i = 0; i < placeholder1.Controls.Count; i++)
placeholder1.Controls.RemoveAt(i);
switch (c)
{
case ControlName.Control1:
placeholder1.Controls.Add(control1);
break;
case ControlName.Control2:
placeholder1.Controls.Add(control2);
break;
case ControlName.Control3:
placeholder1.Controls.Add(control3);
break;
}
}
protected override void CreateChildControls()
{
base.CreateChildControls ();
alert("CreateChildControls");
this.OnControlChange += new ControlChangeEventHandler(this.SetControl);
testplace.Control1 c1 = new testplace.Control1();
c1.button.Click += new EventHandler(Control1_button_Click);
this.control1 = c1.GetControl1;
testplace.Control2 c2 = new testplace.Control2();
c2.button.Click += new EventHandler(Control2_button_Click);
this.control2 = c2.GetControl2;
testplace.Control3 c3 = new testplace.Control3();
c3.button.Click += new EventHandler(Control3_button_Click);
this.control3 = c3.GetControl3;
placeholder1.Controls.Add(control1);
}
private void Page_Load(object obj, EventArgs e)
{
alert("Page_Load");
}
protected override void OnInit(EventArgs e)
{
this.Load += new EventHandler(Page_Load);
alert("OnInit");
base.OnInit (e);
}
private void alert(string str)
{
HttpContext.Current.Response.Write("<script language='javascript'>alert('" + str + "');</script>");
}
private void Control1_button_Click(object sender, EventArgs e)
{
alert("Control1_Button_Clicked");
this.OnControlChange(ControlName.Control2);
}
private void Control2_button_Click(object sender, EventArgs e)
{
alert("Control2_Button_Clicked");
this.OnControlChange(ControlName.Control3);
}
private void Control3_button_Click(object sender, EventArgs e)
{
alert("Control3_Button_Clicked");
}
}
}
=========================================================
The scripts: control1.cs
=========================================================
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.Security;
namespace testplace
{
public class Control1 : Page, INamingContainer
{
private Control ctrl1;
public Button button;
public Control GetControl1
{
get { return ctrl1; }
}
public Control1()
{
ctrl1 = LoadControl("control1.ascx");
InitControl(ctrl1);
}
protected void InitControl(Control c)
{
button = (Button) c.FindControl("Button");
}
}
}
=========================================================
The scripts: control2.cs
=========================================================
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.Security;
namespace testplace
{
public class Control2 : Page, INamingContainer
{
private Control ctrl2;
public Button button;
public Control GetControl2
{
get { return ctrl2; }
}
public Control2()
{
ctrl2 = LoadControl("control2.ascx");
InitControl(ctrl2);
}
protected void InitControl(Control c)
{
button = (Button) c.FindControl("Button");
}
}
}
=========================================================
The scripts: control3.cs
=========================================================
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.Security;
namespace testplace
{
public class Control3 : Page, INamingContainer
{
private Control ctrl3;
public Button button;
public Control GetControl3
{
get { return ctrl3; }
}
public Control3()
{
ctrl3 = LoadControl("control3.ascx");
InitControl(ctrl3);
}
protected void InitControl(Control c)
{
button = (Button) c.FindControl("Button");
}
}
}
=========================================================
The scripts: control1.ascx
=========================================================
<asp:Button Runat="server" ID="Button" Text="Button on control 1" />
=========================================================
The scripts: control2.ascx
=========================================================
<asp:Button Runat="server" ID="Button" Text="Button on control 2" />
=========================================================
The scripts: control3.ascx
=========================================================
<asp:Button Runat="server" ID="Button" Text="Button on control 3" />
=========================================================
The scripts: default.aspx
=========================================================
<%@ Page language="c#" Codebehind="simpleclass.cs" AutoEventWireup="false" Inherits="testplace.simpleclass" %>
<html>
<body>
<form runat="server">
<asp:PlaceHolder Runat="server" ID="placeholder1"/>
</form>
</body>
</html>
Regards and Cheers,
Wernand Hoefsloot
The Netherlands
.NET Developer