Registration Page

Topics: ASP.NET 2.0, Controls
Jun 22, 2009 at 7:25 PM

I wanted a way for users to self register, so I copied the users page to the root and pulled off the grid. I'm an ultimate noob at .Net, and wasn't able to figure out how the master paged was wired, so the example here is hard coded for the StableStart theme. Also noticed that creating a user doesn't automatically log the user in, so I hardcoded a redirect to the login page.

CreateUser.aspx:

<%@ Page Language="C#" MasterPageFile="~/themes/StableStart/site.master" AutoEventWireup="true" CodeFile="CreateUser.aspx.cs" Inherits="admin_newuser" Title="Create new user" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" runat="Server">
    <br />
    <div style="text-align:center; margin:10px;">
        <h1>Register</h1>
        <br />
        <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" LoginCreatedUser="false" align="center">
            <WizardSteps>
                <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" />
            </WizardSteps>
        </asp:CreateUserWizard>
        <asp:Label runat="server" ID="lblError" Text="Username is already taken" style="color:Red" visible="false" />
    </div>
    </asp:Content>

CreateUser.aspx.cs:

#region Using

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BlogEngine.Core.Providers;
using BlogEngine.Core;

#endregion

public partial class admin_newuser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateUserWizard1.CreatingUser += new LoginCancelEventHandler(CreateUserWizard1_CreatingUser);
        CreateUserWizard1.CreatedUser += new EventHandler(CreateUserWizard1_CreatedUser);

        Page.Title = Resources.labels.users;
    }

    void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        if (Membership.GetUser(CreateUserWizard1.UserName) != null)
        {
            e.Cancel = true;
            lblError.Visible = true;
        }
    }

    /// <summary>
    /// Implements a row control finder based on the type of control and the control Id.
    /// </summary>
    /// <param name="row">The row.</param>
    /// <param name="controlType">Type of the control.</param>
    /// <param name="id">Name of the contains.</param>
    /// <returns>The control if found, otherwise null</returns>
    static Control FindRowControl(GridViewRow row, Type controlType, string id)
    {
        foreach (TableCell cell in row.Cells)
        {
            foreach (Control control in cell.Controls)
            {
                if (control.GetType() == controlType && control.ID.Contains(id))
                {
                    return control;
                }
            }
        }
        return null;
    }

    void gridUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && !Page.IsPostBack)
        {
            LinkButton delete = e.Row.Cells[0].Controls[2] as LinkButton;
            if (delete != null)
            {
                Label username = (Label)FindRowControl(e.Row, typeof(Label), "labelUsername");
                string text =
                        string.Format(Resources.labels.areYouSure, Resources.labels.delete.ToLowerInvariant(),
                                                    username.Text.Trim());
                if (Page.User.Identity.Name.Equals(username.Text, StringComparison.OrdinalIgnoreCase))
                {
                    delete.OnClientClick = "alert('You cannot delete your own account');return false;";
                }
                else
                {
                    delete.OnClientClick = "return confirm('" + text.Replace("'", "\\'") + "')";
                }
            }
        }
    }

    private static void UpdatePosts(string oldUsername, string newUsername)
    {
        for (int i = 0; i < Post.Posts.Count; i++)
        {
            Post post = Post.Posts[i];
            if (post.Author == oldUsername)
            {
                foreach (Comment comment in post.Comments)
                {
                    if (comment.Author == oldUsername)
                        comment.Author = newUsername;
                }

                post.Author = newUsername;
                post.Save();
            }
        }
    }

    /// <summary>
    /// Handles the CreatedUser event of the CreateUserWizard1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        Response.Redirect("http://nutribody.com/blog/login.aspx", true);
    }
}

 

Jun 25, 2009 at 11:53 AM

Thanks I was looking to do the same thing.

 

This worked like a Champ!

Sep 3, 2009 at 7:41 PM

Thanks a lot. It worked, however it does not create the user/role record... Any ideas?

 

Nov 2, 2009 at 3:32 AM

I have tried this and when I try to go to the page, it comes up and says opps page(404 error) can not be displayed. I have it all setup for my arthemia theme and no dice. they are in the root directory with the rest of  the content sites. Any help would be greatly appreciated.

#region Using

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BlogEngine.Core.Providers;
using BlogEngine.Core;

#endregion

public partial class admin_newuser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateUserWizard1.CreatingUser += new LoginCancelEventHandler(CreateUserWizard1_CreatingUser);
        CreateUserWizard1.CreatedUser += new EventHandler(CreateUserWizard1_CreatedUser);

        Page.Title = Resources.labels.users;
    }

    void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        if (Membership.GetUser(CreateUserWizard1.UserName) != null)
        {
            e.Cancel = true;
            lblError.Visible = true;
        }
    }

    /// <summary>
    /// Implements a row control finder based on the type of control and the control Id.
    /// </summary>
    /// <param name="row">The row.</param>
    /// <param name="controlType">Type of the control.</param>
    /// <param name="id">Name of the contains.</param>
    /// <returns>The control if found, otherwise null</returns>
    static Control FindRowControl(GridViewRow row, Type controlType, string id)
    {
        foreach (TableCell cell in row.Cells)
        {
            foreach (Control control in cell.Controls)
            {
                if (control.GetType() == controlType && control.ID.Contains(id))
                {
                    return control;
                }
            }
        }
        return null;
    }

    void gridUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && !Page.IsPostBack)
        {
            LinkButton delete = e.Row.Cells[0].Controls[2] as LinkButton;
            if (delete != null)
            {
                Label username = (Label)FindRowControl(e.Row, typeof(Label), "labelUsername");
                string text =
                        string.Format(Resources.labels.areYouSure, Resources.labels.delete.ToLowerInvariant(),
                                                    username.Text.Trim());
                if (Page.User.Identity.Name.Equals(username.Text, StringComparison.OrdinalIgnoreCase))
                {
                    delete.OnClientClick = "alert('You cannot delete your own account');return false;";
                }
                else
                {
                    delete.OnClientClick = "return confirm('" + text.Replace("'", "\\'") + "')";
                }
            }
        }
    }

    private static void UpdatePosts(string oldUsername, string newUsername)
    {
        for (int i = 0; i < Post.Posts.Count; i++)
        {
            Post post = Post.Posts[i];
            if (post.Author == oldUsername)
            {
                foreach (Comment comment in post.Comments)
                {
                    if (comment.Author == oldUsername)
                        comment.Author = newUsername;
                }

                post.Author = newUsername;
                post.Save();
            }
        }
    }

    /// <summary>
    /// Handles the CreatedUser event of the CreateUserWizard1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        Response.Redirect("www.ironbarfitness/login.aspx", true);
    }
}



<%@ Page Language="C#" MasterPageFile="~/themes/arthemia/site.master" AutoEventWireup="true" CodeFile="CreateUser.aspx.cs" Inherits="admin_newuser" Title="Create new user" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cphAdmin" runat="Server">
    <br />
    <div style="text-align:center; margin:10px;">
        <h1>Register</h1>
        <br />
        <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" LoginCreatedUser="false" align="center">
            <WizardSteps>
                <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" />
            </WizardSteps>
        </asp:CreateUserWizard>
        <asp:Label runat="server" ID="lblError" Text="Username is already taken" style="color:Red" visible="false" />
    </div>
    </asp:Content>


Coordinator
Nov 2, 2009 at 3:56 AM

Ooops! I can't find the page you're looking for
http://blogengine.codeplex.com/Thread/View.aspx?ThreadId=66978

Nov 2, 2009 at 6:22 AM

I looked at that and it was already set to off

Jan 22, 2010 at 2:17 PM

In CreateUser.aspx.cs replace:

public partial class admin_newuser : System.Web.UI.Page

with

public partial class admin_newuser : BlogEngine.Core.Web.Controls.BlogBasePage

Ciao

Nov 26, 2010 at 3:26 PM

Hey, I didn't changed:

public partial class admin_newuser : System.Web.UI.Page

by

public partial class admin_newuser : BlogEngine.Core.Web.Controls.BlogBasePage

and everything's working..

what's the difference? May you tell me please?