Friday, February 24

Dynamically Create Multiple Web User Control In a Page With Click Event Functionality

I have created a simple application having 3 user controls as
  • WebUserControl1.ascx
  • WebUserControl2.ascx
  • WebUserControl3.ascx
Also i have a web Page as
  • DynamicUserControl.aspx
First i have  created a  web user control dynamicaaly into the web page but first usercontrol event firing well but for second onwards it needed double click. Finally i got the solution if some one has better then please tell me.

Here is my First WebUsercontrol1.ascx Code:-

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs" Inherits="UserControl_WebUserControl1" %>
<%@ Reference Control="~/UserControl/WebUserControl2.ascx" %>

<asp:Button ID="Next1" runat="server" Text="Next" onclick="Next1_Click" />

WebUsercontrol1.ascx.cs Code:-


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)
        {
            
        }
        protected void Next1_Click(object sender, EventArgs e)
        { 
            Session["test"] = "2";
            Next1Click(sender, e);
            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");
        }
    }
Here is my second WebUsercontrol2.ascx Code:-

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="UserControl_WebUserControl2" %>
<%@ Reference Control="~/UserControl/WebUserControl3.ascx" %>
<asp:Button ID="Next2" runat="server" Text="Next2" onclick="Next2_Click" />



WebUsercontrol2.ascx.cs Code:-


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";
            Next2Click(sender, e);
            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");
        }
        
    }

Here is my Third WebUsercontrol3.ascx Code:-

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl3.ascx.cs" Inherits="UserControl_WebUserControl3" %>
<asp:Button ID="Next3" runat="server" Text="Next3" onclick="Next3_Click" />


WebUsercontrol3.ascx.cs Code:-

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);
          
        }
        
    }


Finally This is my DynamicUserControl.aspx:-

Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicUserControl.aspx.cs" Inherits="DynamicUserControl" %>
<%@ Reference Control="~/UserControl/WebUserControl1.ascx" %>
<%@ Reference Control="~/UserControl/WebUserControl2.ascx" %>
<%@ Reference Control="~/UserControl/WebUserControl3.ascx" %>
<%@ Register Src="~/UserControl/WebUserControl1.ascx" TagName="FirstControl" TagPrefix="gis" %>


<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="ControlHolder" runat="server"></asp:PlaceHolder>
        <br />
        
    </div>
    <asp:HiddenField ID="hf" runat="server" />
   
    </form>
</body>
</html>

My Code Behind:-

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
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["test"] = null;
        }
        int id = 1;
        int i=0;
        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 = null;
           
                ucSimpleControl = (UserControl_WebUserControl2)LoadControl("~/UserControl/WebUserControl" + id + ".ascx");

            
                ControlHolder.Controls.Clear();
                for (i = 1; i <= id; i++)
                {
            ControlHolder.Controls.Add(ucSimpleControl);
                }
            ucSimpleControl.Next2Click += new EventHandler(ucSimpleControl_Next2Click);
        }
        

        if (id == 3)
        {
            UserControl_WebUserControl3 ucSimpleControl=null;
           
             ucSimpleControl = (UserControl_WebUserControl3)LoadControl("~/UserControl/WebUserControl" + id + ".ascx");

            
            ControlHolder.Controls.Clear();
            for (i = 1; i <= id; i++)
            {
            ControlHolder.Controls.Add(ucSimpleControl);
            } 
                ucSimpleControl.Next3Click += new EventHandler(ucSimpleControl_Next3Click);
          
        }

       
            
            
        

    }
    
    protected void ucSimpleControl_Next1Click(object sender, EventArgs e)
    {
        
        Response.Write("The First usercontrol working fine");
    }

    protected void ucSimpleControl_Next2Click(object sender, EventArgs e)
    {
        Response.Write("The Second usercontrol working fine");
    }
    protected void ucSimpleControl_Next3Click(object sender, EventArgs e)
    {
        Response.Write("The Third usercontrol working fine");
    }
   
}

The Screen Shot of my Folder and user control is : 

This is working fine but i need some optimisation way.if any one then let me know.

Thanks Shibashish Mohanty


No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

ShibashishMnty
shibashish mohanty