Dynamic WebParts
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebPartsDynamic.aspx.cs" Inherits="WebPartsDynamic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Web Parts Dynamic</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:WebPartManager ID="WebPartManager1" runat="server" OnDisplayModeChanging="WebPartManager1_DisplayModeChanging">
</asp:WebPartManager>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
RepeatColumns="3">
<asp:ListItem Selected="True">Browse</asp:ListItem>
<asp:ListItem>Edit</asp:ListItem>
<asp:ListItem>Catalog</asp:ListItem>
</asp:RadioButtonList>
<br />
<table>
<tr>
<td>
<asp:WebPartZone ID="WebPartZone1" runat="server">
<ZoneTemplate>
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#999999"
CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="Black" Height="180px" Width="200px">
<SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
<SelectorStyle BackColor="Black" />
<WeekendDayStyle BackColor="#FFFFCC" />
<OtherMonthDayStyle ForeColor="#808080" />
<TodayDayStyle BackColor="Black" ForeColor="Black" />
<NextPrevStyle VerticalAlign="Bottom" />
<DayHeaderStyle BackColor="Black" Font-Bold="True" Font-Size="7pt" />
<TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
</asp:Calendar>
</ZoneTemplate>
</asp:WebPartZone>
</td>
<td>
<asp:WebPartZone ID="WebPartZone2" runat="server" BorderColor="Black" Font-Names="Verdana"
Padding="6">
<PartChromeStyle BackColor="Red" BorderColor="#C5BBAF" Font-Names="Verdana" ForeColor="Navy" />
<MenuLabelHoverStyle ForeColor="Yellow" />
<EmptyZoneTextStyle Font-Size="0.8em" />
<MenuLabelStyle ForeColor="Navy" />
<MenuVerbHoverStyle BackColor="Red" BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px" ForeColor="Navy" />
<HeaderStyle Font-Size="0.7em" ForeColor="Black" HorizontalAlign="Center" />
<ZoneTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
</asp:CheckBoxList>
</ZoneTemplate>
<MenuVerbStyle BorderColor="Yellow" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />
<PartStyle Font-Size="0.8em" ForeColor="Navy" />
<TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />
<MenuPopupStyle BackColor="Yellow" BorderColor="Black" BorderWidth="1px" Font-Names="Verdana"
Font-Size="0.6em" />
<PartTitleStyle BackColor="Yellow" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />
</asp:WebPartZone>
</td>
</tr>
<tr>
<td>
<asp:WebPartZone ID="WebPartZone3" runat="server" BorderColor="Black" Font-Names="Verdana"
Padding="6">
<PartChromeStyle BackColor="Red" BorderColor="#C5BBAF" Font-Names="Verdana" ForeColor="Navy" />
<MenuLabelHoverStyle ForeColor="Yellow" />
<EmptyZoneTextStyle Font-Size="0.8em" />
<MenuLabelStyle ForeColor="Navy" />
<MenuVerbHoverStyle BackColor="Red" BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px" ForeColor="Navy" />
<HeaderStyle Font-Size="0.7em" ForeColor="Black" HorizontalAlign="Center" />
<ZoneTemplate>
<asp:BulletedList ID="BulletedList1" runat="server">
<asp:ListItem>List</asp:ListItem>
<asp:ListItem>Of</asp:ListItem>
<asp:ListItem>Things</asp:ListItem>
</asp:BulletedList>
</ZoneTemplate>
<MenuVerbStyle BorderColor="Yellow" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />
<PartStyle Font-Size="0.8em" ForeColor="Navy" />
<TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />
<MenuPopupStyle BackColor="Yellow" BorderColor="Black" BorderWidth="1px" Font-Names="Verdana"
Font-Size="0.6em" />
<PartTitleStyle BackColor="Yellow" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />
</asp:WebPartZone>
</td>
<td>
<asp:WebPartZone ID="WebPartZone4" runat="server" BorderColor="Black" Font-Names="Verdana"
Padding="6">
<PartChromeStyle BackColor="Red" BorderColor="#C5BBAF" Font-Names="Verdana" ForeColor="Navy" />
<MenuLabelHoverStyle ForeColor="Yellow" />
<EmptyZoneTextStyle Font-Size="0.8em" />
<MenuLabelStyle ForeColor="Navy" />
<MenuVerbHoverStyle BackColor="Red" BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px" ForeColor="Navy" />
<HeaderStyle Font-Size="0.7em" ForeColor="Black" HorizontalAlign="Center" />
<ZoneTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ZoneTemplate>
<MenuVerbStyle BorderColor="Yellow" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />
<PartStyle Font-Size="0.8em" ForeColor="Navy" />
<TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />
<MenuPopupStyle BackColor="Yellow" BorderColor="Black" BorderWidth="1px" Font-Names="Verdana"
Font-Size="0.6em" />
<PartTitleStyle BackColor="Yellow" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />
</asp:WebPartZone>
</td>
</tr>
</table>
</div>
<asp:CatalogZone ID="CatalogZone1" runat="server">
<ZoneTemplate>
<asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />
</ZoneTemplate>
</asp:CatalogZone>
<asp:EditorZone ID="EditorZone1" runat="server">
</asp:EditorZone>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Web Part" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Delete Web Part" />
</form>
</body>
</html>
File: WebPartsDynamic.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class WebPartsDynamic : System.Web.UI.Page
{
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.RadioButtonList1.SelectedValue.ToLower() == "browse")
{
this.WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode;
}
if (this.RadioButtonList1.SelectedValue.ToLower() == "edit")
{
this.WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode;
}
if (this.RadioButtonList1.SelectedValue.ToLower() == "catalog")
{
this.WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Text = "Hello!";
tb.ID = "DynamicWebPart";
GenericWebPart gwp = this.WebPartManager1.CreateWebPart(tb);
this.WebPartManager1.AddWebPart(gwp, this.WebPartZone1, 0);
}
protected void Button2_Click(object sender, EventArgs e)
{
if (this.WebPartZone1.WebParts.Count > 0)
{
this.WebPartManager1.DeleteWebPart(this.WebPartZone1.WebParts[0]);
}
}
protected void WebPartManager1_DisplayModeChanging(object sender, WebPartDisplayModeCancelEventArgs e)
{
if (e.NewDisplayMode == WebPartManager.EditDisplayMode)
{
e.Cancel = true;
e.NewDisplayMode = WebPartManager.BrowseDisplayMode;
this.RadioButtonList1.SelectedIndex = 0;
}
}
}
Related examples in the same category