Dynamic WebParts : WebParts « WebPart « ASP.Net

ASP.Net
1. ADO.net Database
2. Ajax
3. Asp Control
4. Collections
5. Components
6. Data Binding
7. Development
8. File Directory
9. HTML Control
10. Language Basics
11. Login Security
12. Mobile Control
13. Network
14. Page
15. Request
16. Response
17. Server
18. Session Cookie
19. Sitemap
20. Theme Style
21. User Control and Master Page
22. Validation by Control
23. Validation by Function
24. WebPart
25. WPF
26. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.Net » WebPart » WebParts 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.