使用ajax需要配置文件,将配置文件信息与 ASP.NET AJAX 一起使用

本文详细介绍了如何在ASP.NET应用中结合配置文件信息与AJAX,通过启用配置文件服务,允许客户端JavaScript代码读写配置文件属性。首先,要在Web.config中启用配置文件服务,并指定可读写属性。接着,通过示例展示了如何在登录后加载和保存用户配置文件属性,包括背景色和前景色。此外,还提供了加载完成、保存完成和操作失败的回调函数示例。
摘要由CSDN通过智能技术生成

将配置文件信息与 ASP.NET AJAX 一起使用

10/22/2014

本文内容

更新:2007 年 11 月

ASP.NET 配置文件 Web 服务使您能够在 应用程序中使用 ASP.NET 配置文件应用程序服务。本主题演示如何使用 ECMAScript (JavaScript) 代码从浏览器调用应用程序配置文件服务。

使用配置文件信息需要先在网站中启用配置文件服务。这样使其可用作 Web 服务。当启用配置文件 Web 服务时,可通过调用客户端脚本 Sys.Services.ProfileService 类的方法调用它。

启用配置文件服务

若要从脚本使用配置文件服务,必须通过使用应用程序的 Web.config 文件中的下列元素来启用配置文件服务。

< profileService enabled="true" />

默认情况下,没有可用的配置文件属性。对于希望在客户端应用程序中可用的每个配置文件属性 (Property),将属性 (Property) 名称添加到 profileService 元素的 readAccessProperties 属性 (Attribute)。类似地,对于可基于从客户端应用程序发送的数据设置的每个服务器配置文件属性 (Property),将属性 (Property) 名称添加到 writeAccessProperties 属性 (Attribute)。下面的示例演示如何公开各个属性并设置客户端应用程序是否可以读取和写入这些属性。

readAccessProperties="Backgroundcolor,Foregroundcolor"

writeAccessProperties=" Backgroundcolor,Foregroundcolor"/>

通过使用类似于以下示例中的语法,在 profile 节中定义配置文件属性。对于分组的属性,请使用 group 元素。

defaultValue="white" />

defaultValue="black" />

示例

下面的示例演示如何从客户端脚本使用配置文件服务。此示例由包含 ScriptManager 控件的 ASP.NET 网页组成。当页中包含此控件时,Sys.Services.AuthenticationService 对象将自动对此页中的任何客户端脚本可用。

此页包含一个登录按钮和名为 OnClickLogin 的关联的事件处理程序。方法中的代码将调用 AuthenticationService 对象的 login 方法。

登录后,将调用 loginComplete 回调函数,此函数会调用 Sys.Services.ProfileService 对象的 load 方法以加载当前用户的配置文件。

配置文件信息包含登录后可设置的背景色和前景色。背景色和前景色是必须在 Web.config 文件中设置的配置文件属性。若要定义这些属性,请向应用程序的 Web.config 文件添加以下元素:

defaultValue="white"/>

defaultValue="black"/>

示例代码为加载和保存方法提供异步完成的回调函数。也可以为加载和保存方法添加失败的回调函数。

0bccfc35e74eaedf0b3cde0a215a40a0.gif说明:

运行此示例之前,请确保应用程序的成员资格数据库中至少定义了一个用户。有关如何在默认的 SQL Server Express Edition 数据库中创建用户的信息,请参见将 Forms 身份验证用于 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 +=

"
"+ text;

}

function GetElementById(elementId)

{

var element = document.getElementById(elementId);

return element;

}

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

Using Profile Service

body { font: 11pt Trebuchet MS;

padding-top: 72px;

text-align: center }

.text { font: 8pt Trebuchet MS }

Profile Service

You must log in first to set or get profile data.

Create a new user, if needed.
Refer to the Authentication example.

User Name:

id="userId" name="userId" value=""/>

Password:

id="userPwd" name="userPwd" value="" />

id="login" name="login" value="Login"

οnclick="OnClickLogin()" />

value="Set Profile Properties"

οnclick="SetProfileControlsVisibility('visible')"/>

value="Logout" style="visibility:hidden"

οnclick="OnClickLogout()" />

Foreground Color

name="fgcolor" value=""/>

Background Color

name="bgcolor" value="" />

id="saveProf" name="saveProf"

value="Save Profile Properties"

οnclick="SaveProfile();" />

Using Profile Service

body { font: 11pt Trebuchet MS;

padding-top: 72px;

text-align: center }

.text { font: 8pt Trebuchet MS }

Profile Service

You must log in first to set or get profile data.

Create a new user, if needed.

value="Set Profile Properties"

οnclick="SetProfileControlsVisibility('visible')"/>

value="Logout" style="visibility:hidden"

οnclick="OnClickLogout()" />

Foreground Color

name="fgcolor" value=""/>

Background Color

name="bgcolor" value="" />

id="saveProf" name="saveProf"

value="Save Profile Properties"

οnclick="SaveProfile();" />

请参见

任务

概念

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值