Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Wrox Professional Web Parts and Custom Controls with ASP.NET 2.0 phần 9 ppsx
PREMIUM
Số trang
45
Kích thước
960.8 KB
Định dạng
PDF
Lượt xem
1346

Wrox Professional Web Parts and Custom Controls with ASP.NET 2.0 phần 9 ppsx

Nội dung xem thử

Mô tả chi tiết

Working with the

Web Part Architecture

Because this book is focused on creating controls, not a lot of time has been spent on how to use

controls. As an experienced ASP.NET developer, you are already familiar with how to use the vari￾ous ASP.NET server controls. However, Web Parts present a different issue. Not only are Web Parts

the newest part of the ASP.NET toolkit, the way they work together and their dependence on the

ASP.NET personalization sub-system make working with Web Parts a different experience than

working with other ASP.NET controls.

Chapter 2 described how to design pages with Web Parts and how users interact with them. This

chapter describes how a programmer interacts with Web Parts to:

❑ Control which personalization provider is to be used

❑ Set whether changes are applied to the current user or all users

❑ Set and determine which types of changes are permitted on a page

❑ Implement authorization strategies for your Web Parts by creating a custom

WebPartManager

❑ Monitor and manage personalization changes made by the user by interacting with

WebPartManager events

❑ Dynamically convert standard ASP.NET controls on the page to Web Parts and add them

to WebPartZones

❑ Make personalization changes to the host page from the host page’s code

❑ Import and export personalization settings and support importing/exporting a WebPart

that you create

With one exception, none of the material in this chapter directly discusses how to create a control

(the one exception is the section on setting attributes to enable exporting for a Web Part). However,

17_57860x ch11.qxd 10/4/05 9:32 PM Page 353

the more you know about how developers will expect to use your Web Part, the better job you will do

of designing it. And, of course, it’s not unlikely that in addition to building Web Parts, you want to use

them yourself.

Setting Personalization Options

on the WebPartManager

In this section, you learn how to:

❑ Control the personalization options in the WebPartManager

❑ Have changes made by one user shared by many users

❑ Implement authorization for Web Parts

Controlling WebPartManager Personalization Options

You can control much of how personalization is handled by working with the ASP.NET Personalization

object, which can be retrieved from the WebPartManager’s Personalization property. The methods and

properties on this object let you manage the way that personalization is handled on the page:

❑ Switching personalization providers: You can change the personalization provider that is

being used by a page by setting the ProviderName property of the Personalization object (set￾ting up personalization providers was discussed in Chapter 7). This Visual Basic 2005 code sets

the WebPartManager to use the Access provider:

Me.WebPartManager1.Personalization.ProviderName = _

“AspNetAccessPersonalizationProvider”

In C#:

this.WebPartManager1.Personalization.ProviderName =

“AspNetAccessPersonalizationProvider”;.

❑ Discarding personalization changes: You can return a page to its original state by calling the

ResetPersonalizationState method. Before calling this method, you can determine if there are

any changes to be backed out by checking the Personalization object’s HasPersonalizationState

property, which is True if the page has been personalized.

❑ Ensuring that changes are allowed: The Personalization object’s EnsureEnabled will be True

when the personalization infrastructure is fully enabled and ready to accept changes for the cur￾rent user. Setting the Personalization object’s Enabled property to False prevents personalization

changes from being made by the current user. The IsModifiable property allows you to check

whether the current user is allowed to make personalization changes.

You can also disable any personalization changes from being made to a page by setting the

WebPartManager’s Enable property to False.

354

Chapter 11

17_57860x ch11.qxd 10/4/05 9:32 PM Page 354

Applying Changes to Other Users

Personalization changes are made in one of two scopes: shared or user. When the scope is set to user

(the default), the changes made by a user affect that page only when it is requested by that user. To put

it another way: in user scope, a user’s personalization changes are visible to that user only. When the

scope is set to shared, however, changes made to the page are made for all users.

You control the scope of a change by calling the ToggleScope method of the Personalization object. Because

the ToggleScope method switches the scope from whatever its current state is to the other state, you

will usually want to determine the current scope before calling ToggleScope. The current scope can

be determined by testing the Personalization object’s Scope property against one of the enumerated

PersonalizationScope values. Because not all users are allowed to make changes in shared mode, you

should also check the Personalization object’s CanEnterSharedScope property before calling the

ToggleScope method. CanEnterSharedScope returns True if the user is allowed to make shared changes

(or if there is some other reason that shared changes can’t be made). This Visual Basic 2005 code puts

all of these together:

Dim prs As System.Web.UI.WebControls.WebParts.WebPartPersonalization

Dim prs As UI.WebControls.WebParts.WebPartPersonalization;

prs = Me.WebPartManager1.Personalization

If prs.CanEnterSharedScope = True Then

If prs.Scope = PersonalizationScope.User Then

prs.ToggleScope()

End If

End If

In C#:

UI.WebControls.WebParts.WebPartPersonalization prs;

prs = this.WebPartManager1.Personalization;

if(prs.CanEnterSharedScope == true)

{

if(prs.Scope == PersonalizationScope.User)

{

prs.ToggleScope();

}

}

If you do change the WebPartManager’s scope, you can determine the original scope for the

WebPartManager by reading the Personalization object’s InitialState property.

Implementing Authorization

Every Web Part has an AuthorizationFilter property that can be set to any string value. If you want

to take advantage of this property, you must create your own WebPartManager and override either

its OnAuthorizeWebPart or IsAuthorized method. In these methods, you can add code to check the

AuthorizationFilter property on Web Parts and prevent Web Parts from being displayed. These methods

are called automatically, as Web Parts are associated with the WebPartManager on the page.

355

Working with the Web Part Architecture

17_57860x ch11.qxd 10/4/05 9:32 PM Page 355

The following example is a Visual Basic 2005 class that inherits from WebPartManager and overrides the

OnAuthorizeWebPart method. The e parameter passed to the OnAuthorizeWebPart method references

the Web Part being authorized through the WebPart property. You indicate that the Web Part is not

authorized by setting the e parameter’s IsAuthorized property to False:

Public Class PHVWebPartManager

Inherits System.Web.UI.WebControls.WebParts.WebPartManager

Protected Overrides Sub OnAuthorizeWebPart( _

ByVal e As System.Web.UI.WebControls.WebParts.WebPartAuthorizationEventArgs)

If e.AuthorizationFilter <> “Created by PH&V” Then

e.IsAuthorized = False

End If

MyBase.OnAuthorizeWebPart(e)

End Sub

End Class

In C#:

public class PHVWebPartManager : System.Web.UI.WebControls.WebParts.WebPartManager

{

protected override void OnAuthorizeWebPart(

System.Web.UI.WebControls.WebParts.WebPartAuthorizationEventArgs e)

{

if(e.AuthorizationFilter != “Created by PH&V”)

{

e.IsAuthorized = false;

}

base.OnAuthorizeWebPart(e);

}

}

While the OnAuthorizeWebPart currently performs no functions, it’s a good practice to continue to call

the underlying method in case later versions of ASP.NET do implement some default authorization

functionality.

The IsAuthorized method calls the OnAuthorizeWebPart method (unless IsAuthorized has been over￾ridden), so overriding OnAuthorizeWebPart effectively overrides IsAuthorized. However, if you prefer

to override IsAuthorized, the method is passed four parameters:

❑ The type of the Web Part

❑ The path to the Web Part

❑ The Web Part’s AuthorizationFilter

❑ A Boolean isShared parameter that is set to True if the Web Part has its personalization changes

shared among users

356

Chapter 11

17_57860x ch11.qxd 10/4/05 9:32 PM Page 356

Tải ngay đi em, còn do dự, trời tối mất!