Monday, February 10, 2014

ASP.NET: Loading User Control Dynamically in C#

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Load UserControl1" CommandName="UserControl1" OnCommand="btn1_Click" />
        <asp:Button ID="Button2" runat="server" Text="Load UserControl2" CommandName="UserControl2" OnCommand="btn1_Click" />
        <table>
            <tr>
                <td>
                    <asp:PlaceHolder ID="phModule" runat="server"></asp:PlaceHolder>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["UserControl"] = "UserControl1";
            string ControlName = Convert.ToString(Session["UserControl"]);
            LoadControls(ControlName);
        }
        else
        {
            if (Session["UserControl"] != null)
            {
                string ControlName = Convert.ToString(Session["UserControl"]);
                LoadControls(ControlName);
            }
        }
    }
    private void LoadControls(string ControlName)
    {
        string CommandName = ControlName;
        switch (CommandName)
        {
            case "UserControl1":
                phModule.Controls.Clear();
                Control myUserControl = (Control)Page.LoadControl("~/Controls/Uc1.ascx");
                // Place web user control to place holder control
                phModule.Controls.Add(myUserControl);
                break;
            case "UserControl2":
                phModule.Controls.Clear();
                myUserControl = (Control)Page.LoadControl("~/Controls/Uc2.ascx");
                // Place web user control to place holder control
                phModule.Controls.Add(myUserControl);
                break;
        }
    }
    protected void btn1_Click(object sender, CommandEventArgs e)
    {
       
        string CommandName = e.CommandName;
        switch (CommandName)
        {
            case "UserControl1":
                phModule.Controls.Clear();
                Control myUserControl = (Control)Page.LoadControl("~/Controls/Uc1.ascx");
                // Place web user control to place holder control
                phModule.Controls.Add(myUserControl);
                break;
            case "UserControl2":
                phModule.Controls.Clear();
                myUserControl = (Control)Page.LoadControl("~/Controls/Uc2.ascx");
                // Place web user control to place holder control
                phModule.Controls.Add(myUserControl);
                break;
        }
        Session["UserControl"] = CommandName;
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
    }
}

0 comments:

Post a Comment