欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ASP.NET网站开发--个性化用户配置

程序员文章站 2024-03-25 21:17:58
...

1.<Profille>配置节

设置<Profille>配置节经常对其三部分进行设置:

1.<Profille>自身属性;

2.子节<properties>属性设置

3.子节<providers>属性设置

例:

    <profile enabled="true" defaultProvider="AspProfileProvider">
      <providers>
        <clear/>
        <add name="AspProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="aspnetdbConnectionString"/>
      </providers>
      <properties>
        <add name="usename" allowAnonymous="true"/>
        <add name="userpwd" allowAnonymous="true"/>
        <add name="birthday" type="System.DateTime"/>
      </properties>
    </profile>

1.1实例

ASP.NET网站开发--个性化用户配置

Addprofile.aspx页面:

<body>
    <form id="form1" runat="server">
    <div>
    
        Nick Name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        UserPwd:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        Birthday:<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <br />
        <asp:Button ID="Button1" runat="server" Text="保存设置" onclick="Button1_Click" />
    
    </div>
    </form>
</body>

按钮“保存设置”后台单击事件:

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Profile.IsAnonymous)
        {
            Profile.usename = TextBox1.Text;
            Profile.userpwd = TextBox2.Text;
            Profile.birthday = Calendar1.SelectedDate;
            Response.Redirect("Default.aspx");

        }
        else
        {
            Profile.usename = TextBox1.Text;
            Profile.userpwd = TextBox2.Text;
            Response.Redirect("Default.aspx");
 
        }

    }  

Default.aspx.cs页面:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("昵称:" + Profile.usename + "<br/>密码:" + Profile.userpwd + "<br/>生日:"+Profile.birthday.ToLongDateString());

    }

Login.aspx页面:

<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Login ID="Login1" runat="server" DestinationPageUrl="Welcome.aspx" >
        </asp:Login>
    
    </div>
    </form>
</body>

Welcome.aspx页面:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:LoginView ID="LoginView1" runat="server">
        <AnonymousTemplate>
          <a href="Default.aspx">查看个性化设置</a>
        <a href="Addprofile.aspx">添加个性化需求</a>
        </AnonymousTemplate>
        <LoggedInTemplate>
        欢迎:<asp:LoginName ID="LoginName1" runat="server" />
        !!!<br/>
        <a href="Default.aspx">查看个性化设置</a>
        <a href="Addprofile.aspx">添加个性化需求</a>
        </LoggedInTemplate>
        </asp:LoginView>
    
    </div>
    </form>
</body>

web配置文件页:

    <profile enabled="true" defaultProvider="AspProfileProvider">
      <providers>
        <clear/>
        <add name="AspProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="aspnetdbConnectionString"/>
      </providers>
      <properties>
        <add name="usename" allowAnonymous="true"/>
        <add name="userpwd" allowAnonymous="true"/>
        <add name="birthday" type="System.DateTime"/>
      </properties>
    </profile> 

web配置文件页中的:

  <connectionStrings>
    <add name="aspnetdbConnectionString" connectionString="Data Source=.;Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

是连接数据库,并且使用控件绑定数据库所得;(使用后控件可以删除)