<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebPartsDynamic.aspx.cs" Inherits="WebPartsDynamic" %>
Web Parts Dynamic
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;
}
}
}