惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
V
Visual Studio Blog
Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
I
InfoQ
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
P
Proofpoint News Feed
WordPress大学
WordPress大学
小众软件
小众软件
B
Blog
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
量子位
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客

博客园 - L.Zhang

.Net Remoting RMI框架 自己开发连接池 JDBC访问数据库 MyEclipse下开发Web Service 使用传统的XMLHttpRequest发出Ajax请求 XML CDATA XPath 数据库操作的sql脚本 直接选择排序 直接插入排序 气泡排序 Builder 生成器模式(创建型模式) Abstract Factory 抽象工厂模式(创建型模式) Factory Method 工厂方法模式(创建型模式) Singleton单件模式(创建型模式) 服务端如何使用Session 让服务端返回xml 用Get方式访问
使用Profile Service
L.Zhang · 2007-11-01 · via 博客园 - L.Zhang

//客户端代码

<head runat="server">
    
<title>Profile Service</title>
    
<script language="javascript" type="text/javascript">
        
        
//保存profile
        function saveProfiles()
        {   
            
//获取属性对象
            var properties = Sys.Services.ProfileService.properties;
            properties.Name 
= $get("txtName").value;
            properties.Age 
= parseInt($get("txtAge").value, 10);
            properties.Address.City 
= $get("txtCity").value;
            properties.Address.PostalCode 
= $get("txtPostalCode").value;
            
            
/*
            Sys.Services.ProfileService.save(propertyNames, // 需要保存的Profile名,null表示全取
                                             saveCompletedCallback, // 保存成功的回调函数
                                             failedCallback, // 加载失败的回调函数
                                             userContext // 可以随意指定的上下文对象
                                             ); 
*/
            
            
//写入
            Sys.Services.ProfileService.save(
                
null,
                saveCompleted);
        }
        
        
//保存成功回调函数
        function saveCompleted()
        {
            $get(
"message").innerHTML = "Profile Saved";
        }
        
        
//读取profile
        function loadProfiles()
        {
            
/*Sys.Services.ProfileService.load(propertyNames, // 需要加载的Profile名,null表示全取
                                               loadCompletedCallback, // 加载成功的回调函数
                                               failedCallback, // 加载失败的回调函数
                                               userContext // 可以随意指定的上下文对象
                                               );
*/
            Sys.Services.ProfileService.load(
                
null,
                loadCompleted);
        }
        
        
/*
        function loadCompletedCallback(number, // 本次加载的Profile数量
                                       userContext, // 用户随意指定的上下文对象
                                       methodName // 即"Sys.Services.ProfileService.load"
                                      )
        
*/
        
//读取成功回调函数
        function loadCompleted()
        {
            
//获取属性对象
            var properties = Sys.Services.ProfileService.properties;
            
//判断profile是否存在
            if (properties.Age)
            {   
//读取profile
                $get("txtName").value= properties.Name;
                $get(
"txtAge").value= properties.Age;
                $get(
"txtCity").value = properties.Address.City;
                $get(
"txtPostalCode").value = properties.Address.PostalCode;
                
                $get(
"message").innerHTML = "Profile Loaded";
            }
            
else
            {
                $get(
"txtName").value= "";
                $get(
"txtAge").value= "";
                $get(
"txtCity").value = "";
                $get(
"txtPostalCode").value = "";
                
                $get(
"message").innerHTML = "No Profile";
            }
        }
    
</script>
</head>
<body>
    
<form id="form1" runat="server">
        
<asp:ScriptManager ID="ScriptManager1" runat="server">
            
<ProfileService LoadProperties="Address.City, Address.PostalCode, Name, Age" />
        
</asp:ScriptManager>
        
        
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        
<asp:Button ID="btnLogIn" runat="server" Text="Login" OnClick="btnLogin_Click" />
        
<asp:Button ID="btnLogout" runat="server" Text="Logout" OnClick="btnLogout_Click" />
        
<hr />
        
        Name: 
<input type="text" id="txtName" /><br />
        Age: 
<input type="text" id="txtAge" /><br />
        Email: 
<input type="text" id="txtEmail" /><br />
        City: 
<input type="text" id="txtCity" /><br />
        Street: 
<input type="text" id="txtStreet" /><br />
        Postal Code: 
<input type="text" id="txtPostalCode" /><br />
        
        
<div id="message" style="color:Red;"></div><br />
        
        
<input type="button" value="Show" onclick="loadProfiles()" />
        
<input type="button" value="Save" onclick="saveProfiles()" />
    
</form>
</body>

//服务器端函数,用于登陆,登出

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SetAuthCookie(
this.txtUserName.Text, false);
    }
protected void btnLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
    }

//WebConfig配置
出于安全性的考虑,ASP.NET AJAX的Profile Service在默认情况下不会开放Profile的访问权限

  <system.web.extensions>
    
<scripting>
      
<webServices>
        
<profileService enabled="true"
                        readAccessProperties
="Name, Age, Email, Address.City, Address.Street, Address.PostalCode"
                        writeAccessProperties
="Name, Age, Email, Address.City, Address.Street, Address.PostalCode" />
      
</webServices>
    
</scripting>
  
</system.web.extensions>

    <profile enabled="true" automaticSaveEnabled="true" defaultProvider="SqlProvider">
      
<providers>
        
<clear />
        
<add name="SqlProvider"
             type
="System.Web.Profile.SqlProfileProvider"
             connectionStringName
="ProfileDatabase" 
             applicationName
="ProfileSample" 
             description
="Sample for ASP.NET profile and Profile Service" />
      
</providers>
      
<properties>
        
<add name="Name" type="System.String"/>
        
<add name="Email" type="System.String" />
        
<add name="Age" type="System.Int32" />
        
<group name="Address">
          
<add name="City" type="System.String" />
          
<add name="Street" type="System.String" />
          
<add name="PostalCode" type="System.String" />
        
</group>
      
</properties>
    
</profile>