Asp.net网站开发(四)个性化用户设置

个性化用户设置

1.先新建一个网站,然后配置web.config文件

 <connectionStrings>
        <add name="aspnetdbConnectionString" connectionString="Data Source=.;Initial Catalog=aspnetdb;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
      <profile enabled="true" defaultProvider="SqlProfile">
        <providers>
          <clear/>
          <add name="SqlProfile"
               connectionStringName="aspnetdbConnectionString"
               type="System.Web.profile.SqlProfileProvider"
            
            />
        </providers>
        <properties>
          <add name="name" />
          <add name ="date" type="System.DateTime"/>
        </properties>
        
      </profile>

2.在Default.aspx页面

<body>
    <form id="form1" runat="server">
    <div>
     
 <asp:LoginView ID="LoginView1" runat="server">
 <AnonymousTemplate>
        <a href="Default3.aspx">设置个性化</a><br />
       <a href ="Default2.aspx">
          
           获取个性化</a>
 </AnonymousTemplate>
 <LoggedInTemplate>
     <asp:LoginName ID="LoginName1" runat="server" />
        <a href="Default3.aspx">设置个性化</a><br />
       <a href ="Default2.aspx">
          
           获取个性化</a>
 </LoggedInTemplate>
           </asp:LoginView>
    </div>
    </form>
</body>

3,Default3.aspx页面

<body>
    <form id="form1" runat="server">
    <div>
        帐号<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        生日<br />
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <br />
        <asp:Button ID="Button1" runat="server" Text="设置个性化" onclick="Button1_Click" />
    </div>
    </form>
</body>

Default3.aspx.cs

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Profile.IsAnonymous)
        {
            Profile.name = TextBox1.Text;
           Profile.date = Calendar1.SelectedDate;
            Response.Redirect("Default2.aspx");
        }
      
    }

4,Default2.aspx.cs页面

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Profile.IsAnonymous)
        {
            Response.Write(Profile.name+" <br />"+Profile.date.ToString());
        }
    }

匿名个性化配置

1.修改配置web.config文件

注意:启动匿名个性化需要配置节anonymousIdentification,并将属性enable设为true

  <connectionStrings>
        <add name="aspnetdbConnectionString" connectionString="Data Source=.;Initial Catalog=aspnetdb;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
      <anonymousIdentification enabled="true"/>
      <profile enabled="true" defaultProvider="SqlProfile">
        <providers>
          <clear/>
          <add name="SqlProfile"
               connectionStringName="aspnetdbConnectionString"
               type="System.Web.profile.SqlProfileProvider"
            
            />
        </providers>
        <properties>
          <add name="name" allowAnonymous="true"/>
          <add name ="date" type="System.DateTime"/>
        </properties>
        
      </profile>

2.在Default.aspx页面

<body>
    <form id="form1" runat="server">
    <div>
     
 <asp:LoginView ID="LoginView1" runat="server">
 <AnonymousTemplate>
        <a href="Default3.aspx">设置个性化</a><br />
       <a href ="Default2.aspx">
          
           获取个性化</a>
 </AnonymousTemplate>
 <LoggedInTemplate>
     <asp:LoginName ID="LoginName1" runat="server" />
        <a href="Default3.aspx">设置个性化</a><br />
       <a href ="Default2.aspx">
          
           获取个性化</a>
 </LoggedInTemplate>
           </asp:LoginView>
    </div>
    </form>
</body>

扫描二维码关注公众号,回复: 1656306 查看本文章

3,Default3.aspx页面

<body>
    <form id="form1" runat="server">
    <div>
        帐号<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        生日<br />
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <br />
        <asp:Button ID="Button1" runat="server" Text="设置个性化" onclick="Button1_Click" />
    </div>
    </form>
</body>

Default3.aspx.cs

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Profile.IsAnonymous)
        {
            Profile.name = TextBox1.Text;
           Profile.date = Calendar1.SelectedDate;
            Response.Redirect("Default2.aspx");
        }
      else
        {
            Profile.name = TextBox1.Text;
            //Profile.date = Calendar1.SelectedDate;
            Response.Redirect("Default2.aspx");
        }
 }

4,Default2.aspx.cs页面

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Profile.IsAnonymous)
        {
            Response.Write(Profile.name+" <br />"+Profile.date.ToString());
        }
    }

<profile>配置节属性

属性

说明

enabled

Optional Boolean attribute.

指定是否启用 ASP.NET 用户配置文件。如果为 true,则启用 ASP.NET 用户配置文件。

默认值为 true

defaultProvider

可选的 String 属性。

指定默认配置文件提供程序的名称。

有关更多信息,请参见 Provider

默认值为 AspNetSqlProfileProvider

inherits

可选的 String 属性。

包含从 ProfileBase 抽象类派生的自定义类型的类型引用。 ASP.NET 动态地生成一个从该类型继承的 ProfileCommon 类,并将该类放在当前 HttpContextProfile 属性中。

automaticSaveEnabled

可选的 Boolean 属性。

指定用户配置文件是否在 ASP.NET 页执行结束时自动保存。如果为 true,则用户配置文件在 ASP.NET 页执行结束时自动保存。

只有在 ProfileModule 对象检测到某一用户配置文件已修改的情况下,该模块才保存该配置文件。 也就是在 IsDirty 属性为 true 的情况下。 For more information, see ASP.NET 配置文件属性概述.

The default is true.

<properties>子配置节

属性 说明
name 必选的String属性。指定属性名。该值用作自动生成的配置文件类的属性的名称,并用作该属性在Properties集合中的索引值。该属性的名称不能包含句点(。)
type 可选的String属性。指定属性类型。默认值为String
allowAnonymous 可选的Boolean属性。指定在应用程序用户是匿名用户的情况下是否可以获取或设置属性。如果设置为true,则在应用程序用户是匿名用户的情况下可以获取或设置属性。默认值为false
readOnly 可选的Boolean属性。指定是否只能读取而不能设置属性。如果设置为true,则可以读取但不可以设置属性。默认值为false

<providers>子配置节

属性 说明
name 指定提供程序实例的名称。这是用于<profile>元素的defaultProvider属性的值,该值将提供程序实例标识为默认的配置文件提供程序。该提供程序的name还用于在Providers集合中对该提供程序进行索引
type 指定实现ProfileProvider抽象基类的类型
connectionStringName 指定在<connectionStrings>元素中定义的连接字符串的名称。指定的连接字符串将由正在添加的提供程序使用


猜你喜欢

转载自blog.csdn.net/sinat_40900884/article/details/79931048
今日推荐