Click here to Skip to main content
16,012,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my DynamicUserControl.Aspx code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DynamicUserControl : System.Web.UI.Page
{
    //UserControl_WebUserControl1 ucSimpleControl;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["test"] = null;
        }
        int id = 1;
        
        if (Session["test"] != null)
        {
           
            id = int.Parse(Session["test"].ToString());
        
        }
        if (Session["test"] == null)
        {
            UserControl_WebUserControl1 ucSimpleControl = (UserControl_WebUserControl1)LoadControl("~/UserControl/WebUserControl" + id + ".ascx");
            ControlHolder.Controls.Clear();
            ControlHolder.Controls.Add(ucSimpleControl);

            ucSimpleControl.Next1Click += new EventHandler(ucSimpleControl_Next1Click);
        }
        if (id == 2)
        {
            UserControl_WebUserControl2 ucSimpleControl = (UserControl_WebUserControl2)LoadControl("~/UserControl/WebUserControl" + id + ".ascx");
            ControlHolder.Controls.Clear();
            ControlHolder.Controls.Add(ucSimpleControl);

            ucSimpleControl.Next2Click += new EventHandler(ucSimpleControl_Next2Click);
        }
        if (id == 3)
        {
            UserControl_WebUserControl3 ucSimpleControl = (UserControl_WebUserControl3)LoadControl("~/UserControl/WebUserControl" + id + ".ascx");
            ControlHolder.Controls.Clear();
            ControlHolder.Controls.Add(ucSimpleControl);

            ucSimpleControl.Next3Click += new EventHandler(ucSimpleControl_Next3Click);
        }
            
             //ucSimpleControl = (UserControl_WebUserControl1)LoadControl("~/UserControl/WebUserControl"+id+".ascx");
            
        

    }
    
    protected void ucSimpleControl_Next1Click(object sender, EventArgs e)
    {
        Response.Write("It's working");
    }

    protected void ucSimpleControl_Next2Click(object sender, EventArgs e)
    {
        Response.Write("It's working");
    }
    protected void ucSimpleControl_Next3Click(object sender, EventArgs e)
    {
        Response.Write("It's working");
    }
   
}

My FirstUsercontrol.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


    public partial class UserControl_WebUserControl1 : System.Web.UI.UserControl
    {
        public event EventHandler Next1Click;

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["test"] = null;
        }
        protected void Next1_Click(object sender, EventArgs e)
        { 
            Session["test"] = "2";

            UserControl_WebUserControl2 ucSimpleControl = (UserControl_WebUserControl2)LoadControl("~/UserControl/WebUserControl2.ascx");

            PlaceHolder phContactDetails = (PlaceHolder)Page.FindControl("ControlHolder");
            phContactDetails.Controls.Clear();
            phContactDetails.Controls.Add(ucSimpleControl);
            ucSimpleControl.Next2Click += new EventHandler(ucSimpleControl_Next2Click);
          
        }

        protected void ucSimpleControl_Next2Click(object sender, EventArgs e)
        {
            Response.Write("It's working");
        }
    }

my second usercontrol.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
  public partial class UserControl_WebUserControl2 : System.Web.UI.UserControl
    {
        public event EventHandler Next2Click;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Next2_Click(object sender, EventArgs e)
        { 
            Session["test"] = "3";
            UserControl_WebUserControl3 ucSimpleControl = (UserControl_WebUserControl3)LoadControl("~/UserControl/WebUserControl3.ascx");

            PlaceHolder phContactDetails = (PlaceHolder)Page.FindControl("ControlHolder");
            phContactDetails.Controls.Clear();
            phContactDetails.Controls.Add(ucSimpleControl);
            ucSimpleControl.Next3Click += new EventHandler(ucSimpleControl_Next3Click);
           
        }
        protected void ucSimpleControl_Next3Click(object sender, EventArgs e)
        {
            Response.Write("It's working");
        }
    }


my third usercontrol.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


    public partial class UserControl_WebUserControl3 : System.Web.UI.UserControl
    {
        public event EventHandler Next3Click;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Next3_Click(object sender, EventArgs e)
        {
            Next3Click(sender, e);
            //PlaceHolder phContactDetails = new PlaceHolder();
            //phContactDetails.Controls.Add(LoadControl("~/UserControl/WebUserControl3.ascx"));
        }
        protected void UserControl_WebUserControl1_Next3Click(object sender, EventArgs e)
        {
            Response.Write("It's working fine");
        }
    }





please help me why from second user control we have to click the button twice for firing next button events.
Thanks in Advance.

[edit]Code blocks added - OriginalGriff[/edit]
Posted
Updated 25-Feb-12 1:19am
v4
Comments
Ed Nutting 25-Feb-12 9:03am    
What on earth is your code trying to do? Could you explain what you are trying to do (edit your question) and explain, in detail, what order things happen in. Only then could someone (me?) be able to help you...
Varun Sareen 26-Feb-12 9:27am    
exactly
Ariel Riyo 26-Feb-12 21:19pm    
why not construct a single userControl? and just have some statements or arguments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900