WEB开发网
开发学院WEB开发ASP.NET ASP.net 2.0 学习如何用户配置文件(一) 阅读

ASP.net 2.0 学习如何用户配置文件(一)

 2009-11-06 16:50:26 来源:WEB开发网   
核心提示: 概念:PRofile对象提供强类型、可持久化的session状态表单Web配置文件节点代码:<黑白激光传真一体机>view plaincopy to clipboardprint?<profile> <properties> <add name="firstName
 概念:PRofile对象提供强类型、可持久化的session状态表单

Web配置文件节点代码:<黑白激光传真一体机>

view plaincopy to clipboardprint?
<profile>  
<properties>  
<add name="firstName"/>  
<add name="lastName"/>  
<add name="numberOfVisits" type="Int32" defaultValue="0"/>  
</properties>  
</profile> 
<profile>
<properties>
<add name="firstName"/>
<add name="lastName"/>
<add name="numberOfVisits" type="Int32" defaultValue="0"/>
</properties>
</profile>

在Web配置文件中定义Profile后,可以使用Profile对象修改其属性。



asp.net前台界面调用显示结果示例代码:<低速复印机>

view plaincopy to clipboardprint?
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head id="Head1" runat="server"> 
  <title>Show Profile</title> 
</head> 
<body> 
  <form id="form1" runat="server"> 
  <div> 
    
  First Name:  
  <asp:Label 
    id="lblFirstname" 
    Runat="server" /> 
  <br /><br /> 
  Last Name:  
  <asp:Label 
    id="lblLastName" 
    Runat="server" /> 
  <br /><br /> 
  Number of Visits:  
  <asp:Label 
    id="lblNumberOfVisits" 
    Runat="server" /> 
      
  <hr /> 
    
  <asp:Label 
    id="lblNewFirstName" 
    Text="New First Name:" 
    AssociatedControlID="txtNewFirstName" 
    Runat="server" /> 
  <asp:TextBox 
    id="txtNewFirstName" 
    Runat="server" /> 
  <br /><br /> 
  <asp:Label 
    id="lblNewLastName" 
    Text="New Last Name:" 
    AssociatedControlID="txtNewLastName" 
    Runat="server" /> 
  <asp:TextBox 
    id="txtNewLastName" 
    Runat="server" /> 
  <br /><br />    
  <asp:Button 
    id="btnUpdate" 
    Text="Update Profile" 
    OnClick="btnUpdate_Click"  
    Runat="server" /> 
    
  </div> 
  </form> 
</body> 
</html> 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Show Profile</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  
  First Name:
  <asp:Label
    id="lblFirstname"
    Runat="server" />
  <br /><br />
  Last Name:
  <asp:Label
    id="lblLastName"
    Runat="server" />
  <br /><br />
  Number of Visits:
  <asp:Label
    id="lblNumberOfVisits"
    Runat="server" />
    
  <hr />
  
  <asp:Label
    id="lblNewFirstName"
    Text="New First Name:"
    AssociatedControlID="txtNewFirstName"
    Runat="server" />
  <asp:TextBox
    id="txtNewFirstName"
    Runat="server" />
  <br /><br />
  <asp:Label
    id="lblNewLastName"
    Text="New Last Name:"
    AssociatedControlID="txtNewLastName"
    Runat="server" />
  <asp:TextBox
    id="txtNewLastName"
    Runat="server" />
  <br /><br />  
  <asp:Button
    id="btnUpdate"
    Text="Update Profile"
    OnClick="btnUpdate_Click"
    Runat="server" />
  
  </div>
  </form>
</body>
</html>



ASP.NET后台属性赋值示例代码:

view plaincopy to clipboardprint?
void Page_PreRender()  
{  
  lblFirstname.Text = Profile.firstName;  
  lblLastName.Text = Profile.lastName;  
  Profile.numberOfVisits++;  
  lblNumberOfVisits.Text =Profile.numberOfVisits.ToString();  
}  
 
protected void btnUpdate_Click(object sender, EventArgs e)  
{  
  Profile.firstName = txtNewFirstName.Text;  
  Profile.lastName = txtNewLastName.Text;  
} 
void Page_PreRender()
{
  lblFirstname.Text = Profile.firstName;
  lblLastName.Text = Profile.lastName;
  Profile.numberOfVisits++;
  lblNumberOfVisits.Text =Profile.numberOfVisits.ToString();
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
  Profile.firstName = txtNewFirstName.Text;
  Profile.lastName = txtNewLastName.Text;
}



最后理解Profile属性是持久化的非常重要。如果为一个用户设置Profile属性,那么就是该用户过500年也不会返回网站,属性也会保留其值。不想Sess状态。



默认Profile不支持匿名用户,如果需支持匿名用户的话,则在WEB配置文件需另外的配置额外的属性,具体细节,下次马上更新。

Tags:ASP net 学习

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接