共用方式為


搭配 ASP.NET AJAX 使用設定檔資訊

更新:2007 年 11 月

ASP.NET 設定檔 Web 服務可讓您在 應用程式中使用 ASP.NET 設定檔應用程式服務。此主題顯示如何從瀏覽器中使用 ECMAScript (JavaScript) 程式碼呼叫應用程式設定檔服務。

若要使用設定檔資訊,首先必須在網站中啟用設定檔服務。使之成為可用的 Web 服務。啟用設定檔 Web 服務後,可呼叫用戶端指令碼 Sys.Services.ProfileService 類別的方法來叫用該服務。

啟用設定檔服務

若要在指令檔中使用設定檔服務,您必須在應用程式的 Web.config 檔案中使用下列項目來啟用設定檔服務。

<system.web.extensions>
  <scripting>
    <webServices
      < profileService enabled="true" />
    </webServices
  </scripting>
</system.web.extensions>

根據預設,沒有可用的設定檔屬性。針對您要讓用戶端應用程式使用的每一個設定檔屬性,請將屬性 (Property) 名稱加入至 profileService 項目的 readAccessProperties 屬性 (Attribute)。同樣地,針對可根據用戶端應用程式傳送的資料而設定的每一個伺服器設定檔屬性,請將屬性 (property) 名稱加入至 writeAccessProperties 屬性 (attribute)。下列範例顯示如何公開個別的屬性和設定用戶端應用程式是否可讀取和寫入這些屬性。

<profileService enabled="true" 
  readAccessProperties="Backgroundcolor,Foregroundcolor" 
  writeAccessProperties=" Backgroundcolor,Foregroundcolor"/>

在 profile 區段中定義設定檔屬性時,您可以使用類似下列範例中的語法。請使用 group 項目定義群組屬性。

<system.web>
  <profile enabled="true">
    <add name=" Backgroundcolor" type="System.String"
       defaultValue="white" />
    <add name=" Foregroundcolor" type="System.String"
     defaultValue="black" />
    <properties>
      <group name="Address">
       <add name="Street" type="System.String" />
       <add name="City" type="System.String"/>
       <add name="PostalCode" type="System.String" />
      </group>
    </properties>
  </profile>
</system.web>

如需詳細資訊,請參閱HOW TO:設定 ASP.NET AJAX 中的 ASP.NET 服務

範例

下列範例顯示如何在用戶端指令碼中使用設定檔服務。此範例由一個 ASP.NET 網頁構成,這個網頁包含一個 ScriptManager 控制項。在網頁上加入這個控制項時,網頁中的任何用戶端指令碼自動就能使用 Sys.Services.AuthenticationService 物件。

網頁包含的登入按鈕具有相關的事件處理常式,名為 OnClickLogin。這個方法的程式碼會呼叫 AuthenticationService 物件的 login 方法。

在您登入之後,會呼叫 loginComplete 回呼函式,這個函式會呼叫 Sys.Services.ProfileService 物件的 load 方法來載入目前使用者的設定檔。

設定檔資訊由背景和前景色彩構成,您登入後可以設定這些色彩。背景和前景色彩是 Web.config 檔案中必須設定的設定檔屬性。若要定義這些屬性,請將下列項目加入至應用程式的 Web.config 檔案:

<profile enabled="true">
  <properties>
    <add name="Backgroundcolor" type="System.String"
       defaultValue="white"/>
    <add name="Foregroundcolor" type="System.String"
       defaultValue="black"/>
  </properties>
</profile>

範例程式碼為 load 和 save 方法提供非同步完成的回呼函式。您也可以為 load 和 save 方法加入失敗回呼函式。

注意事項:

在執行範例之前,請確定應用程式的成員資格資料庫中至少已定義一個使用者。如需如何在預設 SQL Server Express Edition 資料庫中建立使用者的詳細資訊,請參閱搭配 ASP.NET AJAX 使用表單驗證

var setProfileProps;

function pageLoad()
{
    var userLoggedIn = 
        Sys.Services.AuthenticationService.get_isLoggedIn();
        // alert(userLoggedIn);
        
    profProperties = $get("setProfileProps");
    passwordEntry = $get("PwdId");
    
    if (userLoggedIn == true)
    {
        LoadProfile();
        GetElementById("setProfProps").style.visibility = "visible";
        GetElementById("logoutId").style.visibility = "visible";
    }
    else
    {
        DisplayInformation("User is not authenticated.");
    }
     
}


// The OnClickLogout function is called when 
// the user clicks the Logout button. 
// It logs out the current authenticated user.
function OnClickLogout()
{
    Sys.Services.AuthenticationService.logout(
        null, OnLogoutComplete, AuthenticationFailedCallback,null);
}


function OnLogoutComplete(result, 
    userContext, methodName)
{
    // Code that performs logout 
    // housekeeping goes here.          
}       



// This is the callback function called 
// if the authentication failed.
function AuthenticationFailedCallback(error_object, 
    userContext, methodName)
{   
    DisplayInformation("Authentication failed with this error: " +
        error_object.get_message());
}



// Loads the profile of the current
// authenticated user.
function LoadProfile()
{
    Sys.Services.ProfileService.load(null, 
        LoadCompletedCallback, ProfileFailedCallback, null);

}

// Saves the new profile
// information entered by the user.
function SaveProfile()
{
    
    // Set background color.
    Sys.Services.ProfileService.properties.Backgroundcolor = 
        GetElementById("bgcolor").value;
    
    // Set foreground color.
    Sys.Services.ProfileService.properties.Foregroundcolor =
        GetElementById("fgcolor").value; 
    
    // Save profile information.
    Sys.Services.ProfileService.save(null, 
        SaveCompletedCallback, ProfileFailedCallback, null);
    
}

// Reads the profile information and displays it.
function LoadCompletedCallback(numProperties, userContext, methodName)
{
    document.bgColor = 
        Sys.Services.ProfileService.properties.Backgroundcolor;

    document.fgColor =   
        Sys.Services.ProfileService.properties.Foregroundcolor;         
}

// This is the callback function called 
// if the profile was saved successfully.
function SaveCompletedCallback(numProperties, userContext, methodName)
{
    LoadProfile();
    // Hide the area that contains 
    // the controls to set the profile properties.
    SetProfileControlsVisibility("hidden");
}

// This is the callback function called 
// if the profile load or save operations failed.
function ProfileFailedCallback(error_object, userContext, methodName)
{
    alert("Profile service failed with message: " + 
            error_object.get_message());
}


// Utility functions.

// This function sets the visibilty for the
// area containing the page elements for settings
// profiles.
function SetProfileControlsVisibility(currentVisibility)
{
   profProperties.style.visibility = currentVisibility; 
}

// Utility function to display user's information.
function DisplayInformation(text)
{
    document.getElementById('placeHolder').innerHTML += 
    "<br/>"+ text;
}


function GetElementById(elementId)
{
    var element = document.getElementById(elementId);
    return element;
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

<%@ Page Language="VB" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" >

    <title>Using Profile Service</title>
   <style type="text/css">
        body {  font: 11pt Trebuchet MS;
                padding-top: 72px;
                text-align: center }

        .text { font: 8pt Trebuchet MS }
    </style>

</head>
<body>

    <form id="form1" >
    
        <asp:ScriptManager  ID="ScriptManagerId">
            <Scripts>
                <asp:ScriptReference Path="Profile.js" />
            </Scripts>
        </asp:ScriptManager>


        <h2>Profile Service</h2>
    
        <p>
            You must log in first to set or get profile data. 
            Create a new user, if needed. <br /> Refer to the Authentication example.
        </p>
         
        <div id="loginId" style="visibility:visible">
            <table id="loginForm">
                <tr>
                    <td>User Name: </td>
                    <td><input type="text" 
                        id="userId" name="userId" value=""/></td>
                </tr>
                
                <tr>
                    <td>Password: </td>
                    <td><input type="password" 
                        id="userPwd" name="userPwd" value="" /></td>
                </tr>
                
                <tr>
                    <td><input type="button" 
                        id="login" name="login" value="Login" 
                            onclick="OnClickLogin()" /></td>
                </tr>
            </table>              
    
        </div>
        
        <div id="setProfProps" style="visibility:hidden">
            <input type="button" 
            value="Set Profile Properties" 
            onclick="SetProfileControlsVisibility('visible')"/> 
        </div>
        
        <div id="placeHolder" style="visibility:visible" />
        
        <br />
        
        
        <input id="logoutId" type="button" 
            value="Logout"  style="visibility:hidden"
        onclick="OnClickLogout()" />
        
    
        <div id="setProfileProps" style="visibility:hidden">
            <table>
                <tr>
                    <td>Foreground Color</td>
                    <td><input type="text" id="fgcolor" 
                    name="fgcolor" value=""/></td>
                </tr>
                
                <tr>
                    <td>Background Color</td>
                    <td><input type="text" id="bgcolor" 
                        name="bgcolor" value="" /></td>
                </tr>
                
                <tr>
                    <td><input type="button" 
                    id="saveProf" name="saveProf" 
                    value="Save Profile Properties" 
                    onclick="SaveProfile();" /></td>
                </tr>
            </table>      
        </div>        
    
    </form>

</body>

</html>
<%@ Page Language="C#" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head >

    <title>Using Profile Service</title>
   <style type="text/css">
        body {  font: 11pt Trebuchet MS;
                padding-top: 72px;
                text-align: center }

        .text { font: 8pt Trebuchet MS }
    </style>

</head>
<body>

    <form id="form1" >
    
        <asp:ScriptManager  ID="ScriptManagerId">
            <Scripts>
                <asp:ScriptReference Path="Profile.js" />
            </Scripts>
        </asp:ScriptManager>


        <h2>Profile Service</h2>
    
        <p>
            You must log in first to set or get profile data. 
            Create a new user, if needed. <br /> 
        </p>
         
        <div id="setProfProps" style="visibility:visible">
            <input type="button" 
            value="Set Profile Properties" 
            onclick="SetProfileControlsVisibility('visible')"/> 
        </div>
        
        <div id="placeHolder" style="visibility:visible" />
        
        <br />
        
        
        <input id="logoutId" type="button" 
            value="Logout"  style="visibility:hidden"
        onclick="OnClickLogout()" />
        
    
        <div id="setProfileProps" style="visibility:hidden">
            <table>
                <tr>
                    <td>Foreground Color</td>
                    <td><input type="text" id="fgcolor" 
                    name="fgcolor" value=""/></td>
                </tr>
                
                <tr>
                    <td>Background Color</td>
                    <td><input type="text" id="bgcolor" 
                        name="bgcolor" value="" /></td>
                </tr>
                
                <tr>
                    <td><input type="button" 
                    id="saveProf" name="saveProf" 
                    value="Save Profile Properties" 
                    onclick="SaveProfile();" /></td>
                </tr>
            </table>      
        </div>        
    
    </form>

</body>

</html>

請參閱

工作

HOW TO:設定 ASP.NET AJAX 中的 ASP.NET 服務

概念

在 ASP.NET AJAX 中使用 Web 服務

從用戶端指令碼呼叫 Web 服務

搭配 ASP.NET AJAX 使用表單驗證

Sys.Services.AuthenticationService 類別

Sys.Services.ProfileService 類別