Sunday, July 17, 2011

Dynamically Load User Control Using Ajax Tab Container



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"  TagPrefix="cc1" %>
<%@ Register Src="~/Controls/UC1.ascx" TagName="UC1" TagPrefix="UC1" %>
<%@ Register Src="~/Controls/UC2.ascx" TagName="UC2" TagPrefix="UC2" %>
<%@ Register Src="~/Controls/UC3.ascx" TagName="UC3" TagPrefix="UC3" %>
<!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 id="Head1" runat="server">
    <title></title>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="sc1" runat="server" ></asp:ScriptManager>
    
    <table>
    <tr>
    <td>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    
    
    
    
    <cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" AutoPostBack="true">
    <cc1:TabPanel ID="TabPanel1" runat="server">
    <HeaderTemplate>Home</HeaderTemplate>
    <ContentTemplate>
    <asp:UpdatePanel ID="up1" runat="server">
    <ContentTemplate>
    <asp:Panel ID="panel1" runat="server">
    <asp:Button ID="btn" runat="server" OnClick="btn_Click" Visible="false"/>
    
    
    </asp:Panel>
    </ContentTemplate>
    
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="btn" />
    </Triggers>
    </asp:UpdatePanel>
    
    
    </ContentTemplate>
    </cc1:TabPanel>
    
    <cc1:TabPanel ID="TabPanel2" runat="server">
    <HeaderTemplate>Contact</HeaderTemplate>
    <ContentTemplate>
    <asp:UpdatePanel ID="Up2" runat="server">
    <ContentTemplate>
    <asp:Panel ID="panel2" runat="server">
 <asp:Button ID="btn2" runat="server" OnClick="btn_Click"  Visible="false"/>
    
    </asp:Panel>
    </ContentTemplate>
    
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="btn2" />
    </Triggers>
    </asp:UpdatePanel>
    
    
    </ContentTemplate>
    </cc1:TabPanel>
    
    
    
    <cc1:TabPanel ID="TabPanel3" runat="server">
    <HeaderTemplate>Office Map</HeaderTemplate>
    <ContentTemplate>
    <asp:UpdatePanel ID="Up3" runat="server">
    <ContentTemplate>
    <asp:Panel ID="panel3" runat="server">
 <asp:Button ID="btn3" runat="server" OnClick="btn_Click"  Visible="false"/>
    
    </asp:Panel>
    </ContentTemplate>
    
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="btn3" />
    </Triggers>
    </asp:UpdatePanel>
    
    
    </ContentTemplate>
    </cc1:TabPanel>
    
    
    </cc1:TabContainer>
    
    </ContentTemplate>
    </asp:UpdatePanel>
    </td>
    </tr>
    </table>
    </div>
    
        
    <asp:HiddenField ID="hdnhome" runat="server" />
<asp:HiddenField ID="hdncon" runat="server" />
    
    </form>
</body>
</html>
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 Default2 : System.Web.UI.Page
{
    ASP.controls_uc1_ascx uc1 = new ASP.controls_uc1_ascx();
    ASP.controls_uc2_ascx uc2 = new ASP.controls_uc2_ascx();
    ASP.controls_uc3_ascx uc3 = new ASP.controls_uc3_ascx();
    protected void Page_Load(object sender, EventArgs e)
    {
        btn_Click(sender, e);
    }
    #region btn_Click
    protected void btn_Click(object sender, EventArgs e)
    {
        if (TabContainer1.ActiveTabIndex == 0)
        {
            uc1.LoadControl("~/Controls/UC1.ascx");
            panel1.Controls.Add(uc1);
            ClientScript.RegisterClientScriptBlock(GetType(), "clientscript", "<script> alert('This is Tab1'); </script>", false);
        }
        if (TabContainer1.ActiveTabIndex == 1)
        {
            uc2.LoadControl("~/Controls/UC2.ascx");
            panel2.Controls.Add(uc2);
            ClientScript.RegisterClientScriptBlock(GetType(), "clientscript", "<script> alert('This is Tab2'); </script>", false);
        }
        if (TabContainer1.ActiveTabIndex == 2)
        {
            uc2.LoadControl("~/Controls/UC3.ascx");
            panel3.Controls.Add(uc3);
            ClientScript.RegisterClientScriptBlock(GetType(), "clientscript", "<script> alert('This is Tab2'); </script>", false);
        }
        //panel1.Visible = false;
    }
    #endregion
}

Output :


-------------


usercontrol

0 comments:

Post a Comment