1. Trang chủ
  2. » Công Nghệ Thông Tin

Professional ASP.NET 3.5 in C# and Visual Basic Part 89 doc

10 194 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 396,25 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

After these are in place, you can manipulate how these items appear in all the Web Parts that appear in the chosen Web Part Zone.. In this example, you can see that you simply need to se

Trang 1

<ExportVerb>

<HelpVerb>

<MinimizeVerb>

<RestoreVerb>

Figure 17-18

The<asp:WebPartZone>control allows you to control these verbs by nesting the appropriate verb ele-ments within the<asp:WebPartZone>element itself After these are in place, you can manipulate how these items appear in all the Web Parts that appear in the chosen Web Part Zone

For example, look at graying out the default Close link included with a Web Part This is illustrated in

Listing 17-11

Listing 17-11: Graying out the Close link in a Web Part

<asp:WebPartZone ID="WebPartZone3" Runat="server">

<CloseVerb Enabled="False" />

<ZoneTemplate>

<asp:Calendar ID="Calendar1" Runat="server">

Continued

Trang 2

</ZoneTemplate>

</asp:WebPartZone>

In this example, you can see that you simply need to set theEnabledattribute of the<CloseVerb>

element toFalsein order to gray out the Close link in any of the generated Web Parts included in this

Web Part Zone If you construct the Web Part Zone in this manner, you achieve the results shown in

Figure 17-19

Figure 17-19

If you do not want to gray out the Close link (or any other verb link contained within the Web Part), you

must instead use theVisibleattribute of the appropriate verb (see Listing 17-12)

Listing 17-12: Removing the Close link in a Web Part

<asp:WebPartZone ID="WebPartZone3" Runat="server">

<CloseVerb Visible="False" />

Continued

Trang 3

<asp:Calendar ID="Calendar1" Runat="server">

</asp:Calendar>

</ZoneTemplate>

</asp:WebPartZone>

Using theVisibleattribute produces the screen shown in Figure 17-20

Figure 17-20

Verb elements provide another exciting feature: They give you the capability to use images that would

appear next to the text of an item Using images with the text makes the Web Parts appear more like the overall Windows environment For instance, you can change the contents ofWebPartZone3again so that

it now uses images with the text for the Close and Minimize links This is illustrated in Listing 17-13

Listing 17-13: Using images for the Web Part verbs

<asp:WebPartZone ID="WebPartZone3" Runat="server">

<CloseVerb ImageUrl="Images/CloseVerb.gif" />

Continued

Trang 4

<MinimizeVerb ImageUrl="Images/MinimizeVerb.gif" />

<ZoneTemplate>

<asp:Calendar ID="Calendar1" Runat="server">

</asp:Calendar>

</ZoneTemplate>

</asp:WebPartZone>

To point to an image for the verb, use theImageUrlattribute This produces something similar to

Figure 17-21, depending on the images you use

Figure 17-21

This chapter, thus far, has concentrated on creating completely customizable portal applications in a

declarative manner using the capabilities provided by the ASP.NET Portal Framework As with most

aspects of ASP.NET, however, not only can you work with appearance and functionality in a declarative

fashion, but you can also create the same constructs through server-side code

Trang 5

Wor king with Classes in the Por tal

Framewor k

The Portal Framework provides three main classes for dealing with the underlying framework presented

in this chapter:WebPartManager,WebPartZone, andWebPart

TheWebPartManagerclass allows you to perform multiple operations in your server-side code The

following table shows a partial listing of some of the properties that this class provides

WebPartManager

Class Properties Description

Connections Provides a collection of all the connections between Web Parts contained

on the page

DisplayMode Allows you to change the page’s display mode Possible choices include

CatalogDisplayMode,ConnectDisplayMode,DesignDisplayMode,

EditDisplayMode, andBrowseDisplayMode

SelectedWebPart Allows you to perform multiple operations on the selected Web Part

Beyond the properties of theWebPartManagerclass, you also have an extensive list of available methods

at your disposal The following table outlines some of the available methods of theWebPartManagerclass

WebPartManager

Class Methods Description

page

value

on the page

DisconnectWebParts Allows you to delete a connection between two Web Parts

change the index order in which Web Parts appear in a particular zone

Whereas theWebPartManagerclass allows you to manipulate the location, addition, and deletion of Web Parts that appear in the page as a whole, theWebPartZoneclass allows you to modify a single Web Part Zone on the page The following table provides a list of some properties available to theWebPartZone

class

Trang 6

WebPartZone Class

Properties Description

AllowLayoutChange Takes aBooleanvalue and either enables or disables the Web Part

Zone’s capability to accept or allow any changes in the Web Parts it contains

BackColor,

BackImageUrl,

BorderColor,

BorderStyle,

BorderWidth

Enable you to modify the Web Part Zone’s general appearance

you can then manipulate the verb’sDescription,Enabled,ImageUrl,

Text, andVisibleproperties

manipulate the verb’sDescription,Enabled,ImageUrl,Text, and

Visibleproperties

DragHighlightColor Takes aSystem.Colorvalue that sets the color of the Web Part Zone’s

border if focused when the moving of Web Parts is in operation This also changes the color of the line that appears in the Web Part Zone specifying where to drop the Web Part

manipulate the verb’sDescription,Enabled,ImageUrl,Text, and

Visibleproperties

EmptyZoneText Sets the text that is shown in the zone if a Web Part is not set in the

zone

manipulate the verb’sDescription,Enabled,ImageUrl,Text, and

Visibleproperties

MenuLabelStyle,

MenuLabelText

Enable you to modify the drop-down menu that appears when end users edit a Web Part These properties let you apply an image, alter the text, or change the style of the menu

manipulate the verb’sDescription,Enabled,ImageUrl,Text, and

Visibleproperties

LayoutOrientation Enables you to change the Web Part Zone’s orientation from horizontal

to vertical or vice versa

manipulate the verb’sDescription,Enabled,ImageUrl,Text, and

Visibleproperties

Trang 7

WebPartZone Class

Properties Description

ButtonType.Button,ButtonType.Image, orButtonType.Link

You have a plethora of options to manipulate the look-and-feel of the Web Part Zone and the items

contained therein

The final class is theWebPartclass This class enables you to manipulate specific Web Parts located on

the page The following table details some of the properties available in theWebPartclass

WebPart Class

Properties Description

closed and removed from the page

AllowEdit Takes aBooleanvalue that specifies whether the end user can edit the

Web Part

Web Part within the Web Part Zone If the control is hidden, it is still in the zone, but invisible

AllowMinimize Takes aBooleanvalue that specifies whether the end user can collapse

the Web Part

AllowZoneChange Takes aBooleanvalue that specifies whether the end user can move the

Web Part from one zone to another

BackColor,

BackImageUrl,

BorderColor,

BorderStyle,

BorderWidth

Enable you to modify the Web Part’s general appearance

ChromeState Specifies whether the Web Part chrome is in a normal state or is

minimized

ChromeType Specifies the chrome type that the Web Part uses Available options

includeBorderOnly,Default,None,TitleAndBorder, andTitleOnly

Direction Specifies the direction of the text or items placed within the Web Part

Available options includeLeftToRight,NotSet, andRightToLeft This property is ideal for dealing with Web Parts that contain Asian text that

is read from right to left

Trang 8

WebPart Class

Properties Description

verb Available options includeModal,Modeless, andNavigate.Modal

displays the help items within a modal window if the end user’s browser supports modal windows If not, a pop-up window displays

Modelessmeans that a pop-up window displays for every user

Navigateredirects the user to the appropriate help page (specified by theHelpUrlproperty) when he clicks on the Help verb

specifies the location of the page the end user is redirected to when he clicks on the Help verb

ScrollBars Applies scroll bars to the Web Part Available values includeAuto,

Both,Horizontal,None, andVertical

Title Specifies the text for the Web Part’s title Text appears in the title bar

section

TitleIconImageUrl Enables you to apply an icon to appear next to the title by specifying to

the icon image’s location as aStringvalue of the property

TitleUrl Specifies the location to direct the end user when the Web Part’s title

Web Part is clicked When set, the title is converted to a link; when not set, the title appears as regular text

Zone Allows you to refer to the zone in which the Web Part is located

Creating Custom Web Par ts

When adding items to a page that utilizes the Portal Framework, you add the pre-existing ASP.NET

Web server controls, user controls, or custom controls In addition to these items, you can also build and

incorporate custom Web Parts Using theWebPartsclass, you can create your own custom Web Parts

Although similar to ASP.NET custom server control development, the creation of custom Web Parts adds

some additional capabilities Creating a class that inherits from theWebPartclass instead of theControl

class enables your control to use the new personalization features and to work with the larger Portal

Framework, thereby allowing for the control to be closed, maximized, minimized, and more

To create a custom Web Part control, the first step is to create a project in Visual Studio 2008 From

Visual Studio, choose File ➪New Project This pops open the New Project dialog From this dialog,

select ASP.NET Server Control Name the projectMyStateListBoxand click OK to create the project

You are presented with a class that contains the basic framework for a typical ASP.NET server control

Ignore this framework; you are going to change it so that your class creates a custom Web Parts control

instead of an ASP.NET custom server control Listing 17-14 details the creation of a custom Web Part

control

Trang 9

Listing 17-14: Creating a custom Web Part control

VB

Imports System

Imports System.Web

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Namespace Wrox

Public Class StateListBox

Inherits WebPart

Private _LabelStartText As String = " Enter State Name: "

Dim StateInput As New TextBox

Dim StateContents As New ListBox

Public Sub New()

Me.AllowClose = False End Sub

<Personalizable(), WebBrowsable()> _

Public Property LabelStartText() As String

Get Return _LabelStartText End Get

Set(ByVal value As String) _LabelStartText = value End Set

End Property

Protected Overrides Sub CreateChildControls()

Controls.Clear() Dim InstructionText As New Label InstructionText.BackColor = Drawing.Color.LightGray InstructionText.Font.Name = "Verdana"

InstructionText.Font.Size = 10 InstructionText.Font.Bold = True InstructionText.Text = LabelStartText Me.Controls.Add(InstructionText) Dim LineBreak As New Literal LineBreak.Text = "<br />"

Me.Controls.Add(LineBreak) Me.Controls.Add(StateInput) Dim InputButton As New Button InputButton.Text = "Input State"

AddHandler InputButton.Click, AddressOf Me.Button1_Click Me.Controls.Add(InputButton)

Continued

Trang 10

Dim Spacer As New Literal Spacer.Text = "<p>"

Me.Controls.Add(Spacer) Me.Controls.Add(StateContents) ChildControlsCreated = True End Sub

Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) StateContents.Items.Add(StateInput.Text)

StateInput.Text = String.Empty StateInput.Focus()

End Sub End Class

End Namespace

C#

using System;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

namespace Wrox

{

public class StateListBox : WebPart

{

private String _LabelStartText = " Enter State Name: ";

readonly TextBox StateInput = new TextBox();

readonly ListBox StateContents = new ListBox();

public StateListBox() {

AllowClose = false;

} [Personalizable, WebBrowsable]

public String LabelStartText {

get { return _LabelStartText; } set { _LabelStartText = value; } }

protected override void CreateChildControls() {

Controls.Clear();

Label InstructionText = new Label();

InstructionText.BackColor = System.Drawing.Color.LightGray;

InstructionText.Font.Name = "Verdana";

InstructionText.Font.Size = 10;

InstructionText.Font.Bold = true;

InstructionText.Text = LabelStartText;

Continued

Ngày đăng: 05/07/2014, 19:20