Display Mode Menu : WebPartManager « WebPart « ASP.Net






Display Mode Menu


<%@ Page Language="C#" %>
<%@ Register Src="Control.ascx" TagName="DisplayModeMenul" TagPrefix="uc1" %>

<!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 id="Head1" runat="server">
    <title>Web Parts Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:WebPartManager ID="WebPartManager1" runat="server" />
       <uc1:DisplayModeMenul ID="DisplayModeMenul1" runat="server" />
            <table>
            <tr>
                <td>
                    <asp:WebPartZone ID="WebPartZone1" Runat="server" Width="215px" Height="191px" HeaderText="News">
                        <ZoneTemplate>
                            <asp:Label Runat="server" Title="Today's News" 
                            ID="Label1"><br />Penguin Classics releases new translation of "In Search of Lost Time"</asp:Label>
                        </ZoneTemplate>
                    </asp:WebPartZone>
                </td>
                <td>
                    <asp:WebPartZone ID="WebPartZone2" Runat="server" Width="98px" Height="181px">
                    </asp:WebPartZone>
                </td>
                <td>
                    <asp:WebPartZone ID="WebPartZone3" Runat="server" Width="207px" Height="173px" HeaderText="Sponsors">
                        <ZoneTemplate>
                            <asp:ListBox Runat="server" ID="ListBox1">
                                <asp:ListItem>A</asp:ListItem>
                                <asp:ListItem>B</asp:ListItem>
                                <asp:ListItem>C</asp:ListItem>
                                <asp:ListItem>D</asp:ListItem>
                                <asp:ListItem>E</asp:ListItem>
                                <asp:ListItem>F</asp:ListItem>
                            </asp:ListBox>
                        </ZoneTemplate>
                    </asp:WebPartZone>
                </td>
            </tr>
            <tr>
                <td>
                  <asp:CatalogZone ID="CatalogZone1" runat="server">
                     <ZoneTemplate>
                        <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server">
                           <WebPartsTemplate>
                              <asp:Calendar ID="Calendar1" runat="server" title="Calendar" />
                              <asp:FileUpload ID="FileUpload1" runat="server" title="Upload Files" />
                           </WebPartsTemplate>
                        </asp:DeclarativeCatalogPart>
                     </ZoneTemplate>
                  </asp:CatalogZone>
                </td>
                <td>
                    <asp:WebPartZone ID="WebPartZone5" Runat="server" Width="98px" Height="161px">
                    </asp:WebPartZone>
                </td>
                <td>
                   <asp:EditorZone ID="EditorZone1" runat="server" BackColor="#F7F6F3" BorderColor="Navy" BorderWidth="1px" Font-Names="Verdana" Padding="6" >
                      <ZoneTemplate>
                         <asp:AppearanceEditorPart ID="AppearanceEditorPart1" runat="server" />
                         <asp:LayoutEditorPart ID="LayoutEditorPart1" runat="server" />
                      </ZoneTemplate>
                      <LabelStyle Font-Size="0.8em" ForeColor="Red" />
                      <HeaderStyle BackColor="#E2DED6" Font-Bold="True" Font-Size="0.8em" ForeColor="Red" />
                      <HeaderVerbStyle Font-Bold="False" Font-Size="0.8em" Font-Underline="False" ForeColor="Red" />
                      <PartChromeStyle BorderColor="#E2DED6" BorderStyle="Solid" BorderWidth="1px" />
                      <PartStyle BorderColor="#F7F6F3" BorderWidth="5px" />
                      <FooterStyle BackColor="#E2DED6" HorizontalAlign="Right" />
                      <EditUIStyle Font-Names="Verdana" Font-Size="0.8em" ForeColor="Red" />
                      <InstructionTextStyle Font-Size="0.8em" ForeColor="Red" />
                      <ErrorStyle Font-Size="0.8em" />
                      <VerbStyle Font-Names="Verdana" Font-Size="0.8em" ForeColor="Red" />
                      <EmptyZoneTextStyle Font-Size="0.8em" ForeColor="Red" />
                      <PartTitleStyle Font-Bold="True" Font-Size="0.8em" ForeColor="Red" />
                   </asp:EditorZone>
                </td>
            </tr>
        </table>

    </div>
    </form>
</body>
</html>


File: Control.ascx

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="Control.ascx.cs" 
Inherits="DisplayModeMenu" %>   
  
<div>
  <asp:Panel ID="Panel1" runat="server" 
    Borderwidth="1" 
    Width="230" 
    BackColor="lightgray"
    Font-Names="Verdana, Arial, Sans Serif" >
    <asp:Label ID="Label1" runat="server" 
      Text="&nbsp;Display Mode" 
      Font-Bold="true"
      Font-Size="8"
      Width="120" />
    <asp:DropDownList ID="ddlDisplayMode" runat="server"  
      AutoPostBack="true" 
      EnableViewState="false" 
      Width="120"
      OnSelectedIndexChanged="ddlDisplayMode_SelectedIndexChanged" />
  </asp:Panel>
</div>

File: Control.ascx.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 DisplayModeMenu : System.Web.UI.UserControl
{
   WebPartManager webPartManager;

   public void Page_Init( object sender, EventArgs e )
   {
      Page.InitComplete += new EventHandler( InitComplete );
   }
   public void InitComplete( object sender, System.EventArgs e )
   {
      webPartManager = WebPartManager.GetCurrentWebPartManager( Page );

      String browseModeName = WebPartManager.BrowseDisplayMode.Name;

      foreach ( WebPartDisplayMode mode in
        webPartManager.SupportedDisplayModes )
      {
         String modeName = mode.Name;
         if ( mode.IsEnabled( webPartManager ) )
         {
            ListItem listItem = new ListItem( modeName, modeName );
            ddlDisplayMode.Items.Add( listItem );
         }
      }
   }

   public void ddlDisplayMode_SelectedIndexChanged( object sender,EventArgs e )
   {
      String selectedMode = ddlDisplayMode.SelectedValue;

      WebPartDisplayMode mode =
       webPartManager.SupportedDisplayModes[selectedMode];
      if ( mode != null )
      {
         webPartManager.DisplayMode = mode;
      }
   }

   public void Page_PreRender( object sender, EventArgs e )
   {
      ListItemCollection items = ddlDisplayMode.Items;
      int selectedIndex =
        items.IndexOf( items.FindByText( webPartManager.DisplayMode.Name ) );
      ddlDisplayMode.SelectedIndex = selectedIndex;
   }
}

 








Related examples in the same category

1.Web part with WebPartManager