1) go to solution explorer-->RightClick-->Add New Item-->Add class file--> name it as DynamicWebParts.cs
2)go to code behind and Write as
------
2)go to code behind and Write as
------
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Web.Preview.UI.Controls.WebParts;
/// <summary>
/// Summary description for DynamicWebParts
/// </summary>
/// ******
//to Suspport Microsoft WebpartManager and WebPartZone Create namespace varibles as following
using webpartmanager=Microsoft.Web.Preview.UI.Controls.WebParts.WebPartManager;
using webpartzone=Microsoft.Web.Preview.UI.Controls.WebParts.WebPartZone;
//*************
public class DynamicWebParts
{
/// the page to which components will be added
protected Page _CurrentPage;
public DynamicWebParts(Page apage)
{
_CurrentPage = apage;
}
private webpartmanager _CurrentManager;
public webpartmanager Manager
{
set
{
_CurrentManager = value;
}
}
/// <summary>
/// Create a web part from the specified control and add it to the specified zone.
/// </summary>
/// <param name="aZoneName">the zone to which the control will be added</param>
/// <param name="aControlName">the virtual path to the control file</param>
/// <param name="aWebPartTitle">the title to give the newly created Web Part</param>
/// <returns></returns>
///
public WebPart AddWebPartToZone(string ZoneName,string ControlPath,string WebPartTitle)
{
Control controlobj = _CurrentPage.LoadControl(ControlPath);
controlobj.ID = WebPartTitle.Replace(" ","");
webpartzone Zoneobj = _CurrentManager.Zones[ZoneName] as webpartzone;
if(Zoneobj!=null)
{
GenericWebPart genericWebpartobj = _CurrentManager.CreateWebPart(controlobj);
genericWebpartobj.Title = WebPartTitle;
return _CurrentManager.AddWebPart(genericWebpartobj, Zoneobj, 0);
}
return null;
}
}
then go to Default.aspx in html Write as
------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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>WebPartHomeSep1</title>
<style type="text/css">
.Column
{
float:left;
width:33%;
}
.Zone
{
Width:100%;
border:3 solid black;
}
.fade
{
background-color:Black;
opacity:0.5;
filter:alpha(opacity=50);
}
#ul_Dynwebparts
{
background-color:White;
list-style:none;
}
</style>
</head>
<body>
<form id="form1" runat="server">
//Microsoft.Web.Preview.dll's WebpartManager withtag prefix <MSDLL:WebpartManager>///
<MSDLL:WebPartManager ID="WebPartManager1" runat="server" DeleteWarning="true"></MSDLL:WebPartManager>
//Standard asp.net ScriptManager..///
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
//AJAX ToolKit Model POpup Extender///
<Ajax:ModalPopupExtender runat="server" ID="Modelpopupext1" TargetControlID="lbtn_AddStuff" PopupDragHandleControlID="DivCatalogZone" PopupControlID="DivCatalogZone" DropShadow="false" BackgroundCssClass="fade"></Ajax:ModalPopupExtender>
//lbtn_AddStuff displays a model popup///
<div id="DivHead" runat="server" >
<asp:LinkButton ID="lbtn_AddStuff" runat="server" Text="Add_Stuff"></asp:LinkButton>
</div>
//Panel that Shows the DynamicWebparts Name When we Click on Add_Stuff link button///
<asp:Panel id="DivCatalogZone" runat="server" >
<ul id="ul_Dynwebparts">
<li><asp:LinkButton id="lbtn_SlideShow" runat="server" Text="Slide_Show"></asp:LinkButton> </li>
<li><asp:LinkButton id="lbtn_News_Reel" runat="server" Text="News_Reel"></asp:LinkButton> </li>
<li><asp:LinkButton id="lbtn_Updates" runat="server" Text="Updates"></asp:LinkButton> </li>
<li><asp:LinkButton id="lbtn_Charts" runat="server" Text="Chart"></asp:LinkButton> </li>
<li><asp:LinkButton id="lbtn_Reminder" runat="server" Text="Reminder"></asp:LinkButton> </li>
</ul>
</asp:Panel>
//WebPartZone 1///
<div id="DivLeft" class="Column" >
<MSDLL:WebPartZone ID="Zoneleft" runat="server" class="Zone" >
<ZoneTemplate>
</ZoneTemplate>
</MSDLL:WebPartZone>
</div>
//WebPartZone 2///
<div id="DivMiddle" class="Column">
<MSDLL:WebPartZone ID="ZoneMiddle" HeaderText=" " EmptyZoneText=" " BorderColor="White" runat="server" class="Zone">
<ZoneTemplate>
</ZoneTemplate>
</MSDLL:WebPartZone>
</div>
//WebPartZone 3///
<div id="DivRight" class="Column">
<MSDLL:WebPartZone ID="ZoneRight" runat="server" HeaderText=" " EmptyZoneText=" " BorderColor="White" class="Zone">
<ZoneTemplate>
</ZoneTemplate>
</MSDLL:WebPartZone>
</div>
</form>
</body>
</html>
================================================
in Defauklt.Aspx.Cs Code behind Write as
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Web.Preview.UI.Controls.WebParts;
public partial class Default : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
this.PreRender += delegate
{
if(ApplicationInstance.Context.User.Identity.IsAuthenticated==true)
{
DynamicWebParts dynamicwebpartysobj = new DynamicWebParts(this);
//this code will set the current page webpartmanger1 to the .cs file _currentwebpartmanager field
dynamicwebpartysobj.Manager = WebPartManager1;
}
};
base.OnPreInit(e);
}
///******OnInit Event of the Page********////
protected override void OnInit(EventArgs e)
{
this.PreRender += delegate
{
this.lbtn_Charts.Click += delegate
{
DynamicWebParts dynwebpart = new DynamicWebParts(this);
dynwebpart.Manager = WebPartManager1;
dynwebpart.AddWebPartToZone("Zoneleft", "~/UserControls/ChartUserControl.ascx", "Chart");
};
this.lbtn_News_Reel.Click += delegate
{
DynamicWebParts dynwebpart = new DynamicWebParts(this);
dynwebpart.Manager = WebPartManager1;
dynwebpart.AddWebPartToZone("Zoneleft", "~/UserControls/Scrolling_News.ascx", "Updates");
};
this.lbtn_Reminder.Click += delegate
{
DynamicWebParts dynwebpart = new DynamicWebParts(this);
dynwebpart.Manager = WebPartManager1;
dynwebpart.AddWebPartToZone("Zoneleft", "~/UserControls/Reminder.ascx", "Reminder");
};
this.lbtn_SlideShow.Click += delegate
{
DynamicWebParts dynwebpart = new DynamicWebParts(this);
dynwebpart.Manager = WebPartManager1;
dynwebpart.AddWebPartToZone("Zoneleft", "~/UserControls/SlideShow.ascx", "Slide_Show");
};
base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
No comments:
Post a Comment