ASP.Net/WebPart/WebPartManager

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

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;
   }
}



Web part with WebPartManager

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="WebPartsSample" %>
<!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 Sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:WebPartManager ID="WebPartManager1" runat="server">
        </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" 
                                          DayNameFormat="Shortest" 
                                          Height="180px" 
                                          Width="200px">
                                <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
                                <SelectorStyle BackColor="Navy" />
                                <WeekendDayStyle BackColor="#FFFFCC" />
                                <OtherMonthDayStyle ForeColor="Gray" />
                                <TodayDayStyle BackColor="Navy" ForeColor="Black" />
                                <NextPrevStyle VerticalAlign="Bottom" />
                                <DayHeaderStyle BackColor="Navy" 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="Navy" 
                                     Font-Names="Verdana"
                                     Padding="6">
                        <PartChromeStyle BackColor="#EFF3FB" BorderColor="#D1DDF1" Font-Names="Verdana" ForeColor="Red" />
                        <MenuLabelHoverStyle ForeColor="#D1DDF1" />
                        <EmptyZoneTextStyle Font-Size="0.8em" />
                        <MenuLabelStyle ForeColor="White" />
                        <MenuVerbHoverStyle BackColor="#EFF3FB" 
                                            BorderColor="Navy" 
                                            BorderStyle="Solid"
                                            BorderWidth="1px" 
                                            ForeColor="Red" />
                        <HeaderStyle Font-Size="0.7em" 
                                     ForeColor="Navy" 
                                     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="#507CD1" 
                                       BorderStyle="Solid" 
                                       BorderWidth="1px" 
                                       ForeColor="White" />
                        <PartStyle Font-Size="0.8em" ForeColor="Red" />
                        <TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />
                        <MenuPopupStyle BackColor="#507CD1" 
                                        BorderColor="Navy" 
                                        BorderWidth="1px" 
                                        Font-Names="Verdana"
                                        Font-Size="0.6em" />
                        <PartTitleStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />
                    </asp:WebPartZone>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:WebPartZone ID="WebPartZone3" runat="server" BorderColor="Navy" Font-Names="Verdana"
                        Padding="6">
                        <PartChromeStyle BackColor="#EFF3FB" BorderColor="#D1DDF1" Font-Names="Verdana" ForeColor="Red" />
                        <MenuLabelHoverStyle ForeColor="#D1DDF1" />
                        <EmptyZoneTextStyle Font-Size="0.8em" />
                        <MenuLabelStyle ForeColor="White" />
                        <MenuVerbHoverStyle BackColor="#EFF3FB" 
                                            BorderStyle="Solid"
                                            BorderWidth="1px" 
                                            ForeColor="Red" />
                        <HeaderStyle Font-Size="0.7em" ForeColor="Navy" 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="#507CD1" 
                                       BorderStyle="Solid" 
                                       BorderWidth="1px" 
                                       ForeColor="White" />
                        <PartStyle Font-Size="0.8em" ForeColor="Red" />
                        <TitleBarVerbStyle Font-Size="0.6em" 
                                           Font-Underline="False" 
                                           ForeColor="White" />
                        <MenuPopupStyle BackColor="#507CD1" 
                                        BorderColor="Navy" 
                                        BorderWidth="1px" 
                                        Font-Names="Verdana"
                                        Font-Size="0.6em" />
                        <PartTitleStyle BackColor="#507CD1" 
                                        Font-Bold="True" 
                                        Font-Size="0.8em" 
                                        ForeColor="White" />
                    </asp:WebPartZone>
                </td>
                <td>
                    <asp:WebPartZone ID="WebPartZone4" 
                                     runat="server" 
                                     BorderColor="Navy" 
                                     Font-Names="Verdana"
                                     Padding="6">
                        <PartChromeStyle BackColor="#EFF3FB" 
                                         BorderColor="#D1DDF1" 
                                         Font-Names="Verdana" 
                                         ForeColor="Red" />
                        <MenuLabelHoverStyle ForeColor="#D1DDF1" />
                        <EmptyZoneTextStyle Font-Size="0.8em" />
                        <MenuLabelStyle ForeColor="White" />
                        <MenuVerbHoverStyle BackColor="#EFF3FB" 
                                            BorderColor="Navy" 
                                            BorderStyle="Solid"
                                            BorderWidth="1px" 
                                            ForeColor="Red" />
                        <HeaderStyle Font-Size="0.7em" 
                                     ForeColor="Navy" 
                                     HorizontalAlign="Center" />
                        <ZoneTemplate>
                            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                        </ZoneTemplate>
                        <MenuVerbStyle BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />
                        <PartStyle Font-Size="0.8em" ForeColor="Red" />
                        <TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />
                        <MenuPopupStyle BackColor="#507CD1" 
                                        BorderColor="Navy" 
                                        BorderWidth="1px" 
                                        Font-Names="Verdana"
                                        Font-Size="0.6em" />
                        <PartTitleStyle BackColor="#507CD1" 
                                        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>
    </form>
</body>
</html>

File: Default.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 WebPartsSample : System.Web.UI.Page
{
  protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
  {
    if (this.RadioButtonList1.SelectedValue.ToLower() == "browse")
    {
      this.WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode;
    }
    else if (this.RadioButtonList1.SelectedValue.ToLower() == "edit")
    {
      this.WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode;
    }
    else
    {
      this.WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode;
    }
  }
}