Sunday, July 3, 2011

Dynamically Load User Control in Asp.Net

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Panel.aspx.cs" Inherits="Panel" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>
<%@ Register Src="~/UserControl/Menu.ascx" TagName="Menu" TagPrefix="cc1" %>
 <%@ Reference Control="~/UserControl/Emp1.ascx"  %>
   <%@ Reference Control="~/UserControl/Emp2.ascx" %>
  <%@ Reference Control="~/UserControl/Emp3.ascx" %>
<!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>Untitled Page</title>
    <style type="text/css">
    .Button
       {
        background-color: #316ac4;
        width:75px;  
        height:25px;          
        padding: 0;
        color:Black;
        cursor:pointer;
        color:White;
        font-weight:bold; 
        margin-top:0px;
         
         -moz-border-radius: 15px;
         -webkit-border-radius: 15px;
         -khtml-border-radius: 15px;
          behavior: url(/border-radius.htc);  border-radius: 15px;
          
         }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:ScriptManager ID="sc1" runat="server"></asp:ScriptManager>
        <table border="0" width="700px" bgcolor="#316ac4">
        <tr>
        <td>
         <asp:UpdatePanel ID="UpdatePanel4" runat="server">
       <ContentTemplate>
         <asp:Button ID="Button1" runat="server" Text="Home"  CssClass="Button" OnClick="Button1_Click"/>
          <asp:Button ID="Button2" runat="server" Text="Contact"  CssClass="Button" OnClick="Button2_Click"/>
 <asp:Button ID="Button3" runat="server" Text="Phone"  CssClass="Button" OnClick="Button3_Click"/>
        </ContentTemplate>
        </asp:UpdatePanel>
        </td>
        </tr>
     </table>
       
       <table border="1"  width="700px" height="500px">
       <tr>
       <td>
       <asp:Panel ID="Panelmain" runat="server">
       <asp:UpdatePanel ID="UpdatMain" runat="server">
       <ContentTemplate>
       <asp:Panel ID="Panel1" runat="server">
         <asp:UpdatePanel ID="UpdatePanel1" runat="server">
          <ContentTemplate>
           <%-- Add Text Here--%>
            
        </ContentTemplate>
          </asp:UpdatePanel>
           
       </asp:Panel>
       
       <asp:Panel ID="Panel2" runat="server">
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
          <ContentTemplate>
               <%-- Add Text Here--%>
          </ContentTemplate>
        </asp:UpdatePanel>
       </asp:Panel>
       
       
        <asp:Panel ID="Panel3" runat="server">
        <asp:UpdatePanel ID="UpdatePanel3" runat="server">
          <ContentTemplate>
                <%-- Add Text Here--%>
          </ContentTemplate>
        </asp:UpdatePanel>
       </asp:Panel>
       
       </ContentTemplate>
       </asp:UpdatePanel>
       
       </asp:Panel>
       </td>
       </tr>
       </table>
        
    </div>
    </form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Panel : System.Web.UI.Page
{
    protected ASP.usercontrol_emp1_ascx control1;
    protected ASP.usercontrol_emp2_ascx control2;
    protected ASP.usercontrol_emp3_ascx control3;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
        } 
    }
    //Button1_Click
    protected void Button1_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;
        control1 = (ASP.usercontrol_emp1_ascx)LoadControl("~/UserControl/Emp1.ascx");
        UpdatePanel1.ContentTemplateContainer.Controls.Add(control1);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        control2 = (ASP.usercontrol_emp2_ascx)LoadControl("~/UserControl/Emp2.ascx");
        UpdatePanel2.ContentTemplateContainer.Controls.Add(control2);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
        control3 = (ASP.usercontrol_emp3_ascx)LoadControl("~/UserControl/Emp3.ascx");
        UpdatePanel3.ContentTemplateContainer.Controls.Add(control3);
    }
}
Output:

dynamic

0 comments:

Post a Comment