komatu

       暗  香  堂
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#2.0学习笔记(4)

Posted on 2006-03-30 11:07  暗香堂  阅读(316)  评论(0)    收藏  举报
 

36In combination with VS .NET, you can now design the Content Pages visually within the Master Pages context and fill one or several defined placeholders:

<%@ page language="C#" master="~/MasterPage1.master" %>

Instead of defining individual Master Pages with the @Page directive for each page, you may act more globally and define an undocumented attribute in the web.config file:

<pages   master="~/MasterPage.master" />

37If you want to work with events within the Master Page and/or the Content Page, you should watch out for the correct sequence:

 

Master Page child controls initialization.

Content Page child controls initialization.

Master page initialization.

Content Page initialization.

Content Page load.

Master Page load.

Master Page child controls load.

Content Page child controls load.

38If you intend to define meta tags dynamically, I recommend you use the page's Header object, which implements IPageHeader.

<script runat="server" language="c#">

void Page_Load(object sender, System.EventArgs e)

{

   this.Master.HtmlTitle = "Hello World!";

   this.Header.Metadata.Add("Author", "Patrick A. Lorenz");

}

</script>

39It's possible to nest Master Pages:

<%@ master language="C#" master="~/MasterMasterPage5.master" %>

40it's actually possible to combine user controls and Master Pages :

<%@ control language="C#" classname="UCMaster" master="~/UCMaster.master" %>

41ASP.NET's integrated support for defining a site map is based on an XML file named web.sitemap, which is placed in the root directory of the application. The structure of the file complies with a specific schema:

 

<siteMap>

   <siteMapNode title="" description="" url="">

      <siteMapNode .../>

   </siteMap>

</siteMap>

 

42display of the page's position in the navigation hierarchy

<asp:sitemappath id="SiteMapPath1" runat="server" RenderCurrentNodeAsLink=false>

</asp:sitemappath>

43If you want to build a new provider, you must first create a class that implements the ISiteMapProvider interface. This interface defines three methods and four properties that are used by the SiteMap class to form the desired object model.

44Now you have the option to automatically assign an ID to unknown users:

this.Request.AnonymousId

45If you want to use SQL Server for the storage of membership and role data, you'll have to create a corresponding database first, including some tables. Instead of doing this manually, you can work more comfortable by using the shipped aspnet_regsql.exe tool, which you can find in this directory:

<windir>\Microsoft.NET\Framework\<version>

After the database has been created, web.config

<?xml version="1.0"?>

<configuration>

 

    <connectionStrings>

          <add name="LocalSqlServer"

              connectionString="data source=127.0.0.1;Integrated Security=SSPI" />

    </connectionStrings>

 

    <system.web>

        <membership defaultProvider="AspNetSqlProvider" />

        <roleManager enabled="true" defaultProvider="AspNetSqlProvider" />

    </system.web>

</configuration>

 

you can expand as you desire Your new provider has only to support the IMembershipProvider IRoleProvider interfaces and be deposited in the web.config file.

46Creating a Role-Based Navigation System:

<?xml version="1.0" encoding="utf-8" ?>

<siteMap>

   <siteMapNode title="Home" description="Home" url="default.aspx" >

      <siteMapNode title="Admin"

          description="For admins only"

          url="admin.aspx"

          roles="Admins,SuperUsers"

      />

   </siteMapNode>

</siteMap>

47Personalization is activated by default and is used for all authenticated users. Before using personalization, you must specify which data you would like to save individually for each user. The definition of such a profile takes place in the <personalization> section of the web.config configuration file. As shown in the following example, you may place individual properties here, such as a nickname. In principle, you can define as many profile properties as you want by simply using the corresponding <property> tags.

 

<?xml version="1.0"?>

<configuration>

    <system.web>

        <personalization>

            <profile>

                <property name="Nickname" />

            </profile>

         </personalization>

     </system.web>

</configuration>

48personalization is based on a provider model. ASP.NET ships with two providers, one for Microsoft Access and another for SQL Server. Although Microsoft Access is set as the default, I recommend using SQL Server, or at least it's MSDE version, for bigger projects.

49Accessing Somebody Else's Profile,Even write access is possible. In this case, however, changes won't merely be transferred automatically but have to be saved explicitly by calling the CommitSettings() method against the underlying data source:

HttpPersonalization profile = this.Profile.GetProfile("Patrick");

profile.Nickname = "Mr. Ed";

profile.CommitSettings();

50All objects are serialized as a string by default. This is quite a new concept, With reference types, however, string serialization isn't advisable. Of course, this doesn't apply for the string type itself, which is basically a reference type, too. You should choose serialization in the form of XML or binary instead. You can assign the desired setting (String, Binary, or Xml) to the serializeAs attribute.

The following example shows the allocation of a StringCollection that is serialized in the database as XML. The StringCollection is available in the profile through the property Bookmarks.

<personalization>

    <profile>

        <property name="Nickname" />

        <property name="Birthday" type="System.DateTime" />

        <property name="YearsExperienceASPNET" type="int" defaultValue="1"/>

        <property name="Bookmarks"

            type="System.Collections.Specialized.StringCollection"

            serializeAs="Xml"/>

    </profile>

</personalization>

51You can store any individual data in a profile. But again, this will work only under the condition that your class supports serialization.

[Serializable]

public class Basket : System.Collections.Generic.List<BasketItem>

{…Add()…}

 

Then

<personalization>

    <profile>

        <property name="Basket" type="Basket" serializeAs="Xml" />

    </profile>

</personalization>

 

Done! From now on, you can save new items in the profile of the user, as this example demonstrates:

this.Profile.Basket.Add(new BasketItem(1, "Hello world product", 5, 12.34m));

52you can mark custom properties of the profile so that they can be saved even if the user is anonymous.

<personalization>

    <profile>

        <property name="Nickname" />

        <property name="Birthday" type="System.DateTime" />

        <property name="YearsExperienceASPNET" type="int" defaultValue="1" />

 

        <property name="Bookmarks"

            type="System.Collections.Specialized.StringCollection"

            serializeAs="Xml"

            allowAnonymous="true"

        />

 

        <property name="Basket"

            type="Basket"

            serializeAs="Xml"

            allowAnonymous="true"

        />

    </profile>

</personalization>

53The migration can be handled in the MigrateAnonymous event of the class PersonalizationModule:

<%@ application language="C#" %>

<script runat="server">

void Personalization_MigrateAnonymous(object sender,

    PersonalizationMigrateEventArgs e)

{

    HttpPersonalization oldProfile =

         ((HttpPersonalization) e.Context.Profile).GetProfile(e.AnonymousId);

    HttpPersonalization newProfile = (HttpPersonalization) e.Context.Profile;

    // Merging Bookmarks

    string[] bookmarks = new string[oldProfile.Bookmarks.Count];

    oldProfile.Bookmarks.CopyTo(bookmarks, 0);

    newProfile.Bookmarks.AddRange(bookmarks);

    // Merging Basket

    newProfile.Basket.AddRange(oldProfile.Basket);

}

</script>

54WebPartManager.DisplayMode

设置或者获取页面的显示模式

BrowserDisplayMode “正常的” 显示模式,无法编辑(默认)

DesignDisplayMode 允许拖拽式布局编辑

EditDisplayMode 允许编辑Web Part的外观及行为

CatalogDisplayMode 允许将Web Part添加在另外的页面上

ConnectDisplayMode 允许Web Parts之间进行通讯

55WebParts设置的信息保存在aspnet_PersonalizationPerUser

56Applying Themes to a Web Site Area

Quite often you'll want to use different Themes for individual areas of a web site. In this case, you have two possibilities that the configuration system of .NET offers to you in the familiar way:

You can store the subsections in separate directories and provide each of them with its own web.config configuration file that inherits the configuration of the root directory and overwrites the chosen Theme.

Alternatively, you can use the <location> tag to define different Themes for a given path (either directory or page) within the root web.config file.

 

The following snippet shows the application of the <location> tag, which is used to assign an alternative Theme to the subdirectory named "subfolder".

 

<?xml version="1.0" encoding="UTF-8" ?>

 

<configuration>

    <system.web>

        <compilation debug="true" />

        <pages theme="BasicBlue" />

   </system.web>

 

    <location path="subfolder">

        <system.web>

            <pages theme="SmokeAndGlass" />

        </system.web>

    </location>

 

</configuration>

57Applying Themes to a Single Page:

<%@ page language="C#" theme="SmokeAndGlass" %>

or

void Page_PreInit(object sender, System.EventArgs e)

{

    this.Theme = "SmokeAndGlass";

}

58.当 ASP.NET 2.0 BETA 2 版本发布后,您就需要将此文件夹命名为 App_Themes,即在web项目的目录中创建App_Themes后创建XXX目录(例如:SmokeAndGlass),然后在其中建立.skin文件,文件名和子目录不需要相同。一个XXX子目录下可以有多个skin文件,但是其中不能重复定义。Skin文件中定义控件样式时候注意不要设置ID。但是可以增加SkinID区分不同的风格,例如同是button控件可以有不同的SkinID,进而在具体界面上可以设置skinid

59Disabling Themes:EnableTheming="false"

60Using Themes with Personalization:

this.Theme = Profile.Theme;

61Client Callback is applied in this situation. I'll go into the details of this feature in Chapter 11. In summary, this feature allows you to call a server-side method with JavaScript and without a postback of the whole page.

62Using Themes for Custom Controls

[Themeable]

public string MyProperty

{

    get { return this.myProperty; }

    set { this.myProperty = value; }

}

63New Page Events

The Page class offers some new events that allow you even more interaction with the page's life cycle:

PreInit is raised before the initialization of the page. At this time, you may, for example, still define the desired Theme.

InitComplete is raised after the initialization is finished.

PreLoad is raised before the loading of the page.

LoadComplete follows the loading of the page.

PreRenderComplete is raised as soon as the PreRender event is completely handled. Here, for the last time you have the chance to make changes on the page before it's rendered.

64Adding Default Namespaces

<?xml version="1.0"?>

<configuration>

    <system.web>

        <pages>

            <namespaces>

                <add namespace="System.IO" />

            </namespaces>

        </pages>

    </system.web>

</configuration>

65<form id="Form1" runat="server" defaultbutton="" defaultfocus="" >

66.可以将类的关系图导出来