在ASP.NET Atlas中使用Profile Service儲存使用者資訊)

來源:互聯網
上載者:User
 在ASP.NET Atlas中使用Profile Service儲存使用者資訊

English Version: http://me.dflying.net/2006/04/using-profile-service-in-aspnet-atlas.html
作者:Dflying Chen (http://dflying.cnblogs.com/)

我們知道ASP.NET 2.0提供了內建的profile功能支援,用來存放使用者的個人資訊。Atlas在用戶端以AJAX的方式擴充了這個功能。使用Atlas中的profile功能,您可以在用戶端用JavaScript訪問到使用者的profile資料,並可在用戶端進行修改並在適當時候將修改後的資料提交回伺服器儲存,而這一切操作都是採用AJAX的方式完成的。

Atlas中的profile對象是Sys._Profile類的一個執行個體,Sys.Profile,它代表了目前使用者的profile。Atlas同樣提供了一個用戶端控制項Profile,您可以使用它將其他Atlas控制項與當前profile綁定。

Sys.Profile對象有如下屬性:

   1. autoSave:布爾值,表示當profile資料被修改後是否自動認可回伺服器。
   2. initialData:初始的profile資料,將和頁面一起傳輸到用戶端。
   3. isDirty:布爾值,表示當前的profile資料是否被修改過,並尚未提交到伺服器。
   4. propertyNames:Name-Value的集合,存放當前的profile資料。例如,若需要訪問profile中的FirstName屬性,您可以使用Sys.Profile.properties.FirstName或Sys.Profile.properties["FirstName"]兩種方式。

還有如下方法:

   1. load:從伺服器取得目前使用者的profile資料。
   2. save:將用戶端的使用者的profile資料提交回伺服器。

如下事件:

   1. loaded:當取得profile資料完成時被引發。
   2. saved:當提交profile資料完成時被引發。

使用Sys.Profile對象提供的上述屬性/方法/事件,我們可以很容易的編寫高度可自訂的Atlas應用程式。現在讓我們通過一個樣本程式來熟悉Sys.Profile的使用。

該執行個體程式將允許我們取得並顯示自己的profile資料,並可在修改後提交回伺服器儲存。其中定義的profile很簡單,見如下web.config定義:
<profile>
  <properties>
    <add name="FirstName" />
    <add name="LastName" />
  </properties>
</profile>

我們同樣需要在web.config中啟用相應的服務:
<configSections>
  <sectionGroup name="microsoft.web" type="Microsoft.Web.Configuration.MicrosoftWebSectionGroup">
    <section name="webServices" type="Microsoft.Web.Configuration.WebServicesSection" requirePermission="false"/>
    <section name="profileService" type="Microsoft.Web.Configuration.ProfileServiceSection" requirePermission="false"/>
  </sectionGroup>
</configSections>

還有,在microsoft.web段中:
<webServices enableBrowserAccess="true"/>
<profileService enabled="true"     setProperties="FirstName;LastName" getProperties="FirstName;LastName" />

注意到通過上面的設定,我們使FirstName和LastName屬性在用戶端可見。然後我們在Membership資料庫中添加幾個測試的使用者,您可以通過ASP.NET Web Site Administration Tool來設定並添加使用者。

設定檔的工作到此為止,我們來建立一個ASP.NET頁面,並在其中加入一個ScriptManager控制項:
<atlas:ScriptManager ID="scriptManager" runat="server" />

然後加入一個包含兩個input的HTML table,用來顯示,修改使用者的profile資料:
<table id="tbProfile" style="border: 1px solid black;">
    <tr align="center">
        <td colspan="2"><b>Your Profile Values</b></td>
    </tr>
    <tr>
        <td>First name:</td>
        <td><input type="text" id="txtFirstName" /></td>
    </tr>
    <tr>
        <td>Last name:</td>
        <td><input type="text" id="txtLastName" /></td>
    </tr>
</table>

還有一個按鈕用來觸發儲存操作:
<input type="button" id="update" value="Update!" />

在這個樣本程式中為簡單起見,我們僅僅的使用一個ASP.NET伺服器控制項Login來實現使用者登入,它會引發一次PostBack。如果您需要以AJAX的方式登入,請參考在ASP.NET Atlas中結合Membership進行身分識別驗證。
<asp:Login ID="Login1" runat="server">
</asp:Login>

下面是一些JavaScript代碼,用來相應事件並控制Sys.Profile對象:
function OnLoad() {
    Sys.Profile.saved.add(onSaveComplete);
}
function OnSave() {
    Sys.Profile.save();
}
function onSaveComplete() {
    alert('The profile has been saved.');
}

下面是Atlas的XML指令碼。我們可以看到在頁面裝載完成後,開始執行OnLoad()方法,其中添加了儲存profile完成後的事件處理函數。在update按鈕被點擊時,OnLoad()方法將被執行,將用戶端的profile提交回伺服器。
<script type="text/xml-script">
    <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
        <components>
            <profile id="profilecontrol" autoSave="false" />
           
            <textBox id="txtFirstName" >
                <bindings>
                    <binding dataContext="profilecontrol" dataPath="FirstName" property="text" direction="InOut" />
                </bindings>
            </textBox>
            <textBox id="txtLastName" >
                <bindings>
                    <binding dataContext="profilecontrol" dataPath="LastName" property="text" direction="InOut" />
                </bindings>               
            </textBox>
           
            <button id="update">
                <click>
                    <invokeMethod target="application" method="OnSave" />
                </click>
            </button>
           
            <application id="application" load="OnLoad">
            </application>
        </components>
    </page>
</script>

上面同樣需要注意的是我們設定了上述綁定的綁定方向為InOut,這樣對input中資料的改變將被自動反射到實際的profile資料中。

OK, 瀏覽器中測試一下,頁面裝載:

Dflying登入後,您可以看到兩個input已經填充了我的profile資訊:

修改這兩個input的內容,把我升級成Bill Gates。呵呵,一秒鐘以內就搞定了。(yy中……)

以上樣本程式可以在此下載:http://files.cnblogs.com/dflying/AtlasProfileTest.zip

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.