/admin/Pages/Users.aspx

Topics: ASP.NET 2.0, Controls, Themes
Mar 26, 2009 at 8:44 PM
Edited Mar 26, 2009 at 8:45 PM
Could someone show me how to strip everything from the users.aspx page except username and email, I would like to create another page with this info for members.  I have tried but run into errors. Thank you for your help,
Coordinator
Mar 27, 2009 at 3:45 AM
In users.aspx, delete the Password and Roles columns out of the GridView:

==== Password column to delete ====
<asp:TemplateField HeaderText="<%$ Resources:labels, password %>">
  <ItemTemplate>
    **********
  </ItemTemplate>
</asp:TemplateField>

==== Roles column to delete ====
<asp:TemplateField HeaderText="Roles" ItemStyle-Wrap="false" ItemStyle-Width="100%" />                

In users.aspx, an OnLoad handler is defined for the GridView.  It looks like ...

OnLoad="gridUsers_Load"

Delete that since the Load event is only used to populate the roles in the GridView -- but we've already deleted the Roles column.

You can optionally delete the gridUsers_Load() handler in users.aspx.cs too since it won't be used.  You can also optionally delete the cb_CheckedChanged() method in users.aspx.cs since that won't be used anymore too.

That should be it ...
Mar 27, 2009 at 4:51 AM
This is what is left in the users.aspx and wouldn't mind getting rid of the edit/delete fields also.
<asp:GridView runat="server" ID="gridUsers" AutoGenerateColumns="false" UseAccessibleHeader="true" CellPadding="4"
             HeaderStyle-HorizontalAlign="left" DataKeyNames="username" OnLoad="gridUsers_Load" AlternatingRowStyle-CssClass="alt" 
             AutoGenerateEditButton="true" AutoGenerateDeleteButton="true">
            <Columns>
                
                <asp:TemplateField HeaderText="<%$ Resources:labels, username %>">
                  <ItemTemplate>
                    <asp:Label ID="labelUsername" runat="server" Text='<%# Server.HtmlEncode(Eval("username").ToString()) %>' ></asp:Label>
                  </ItemTemplate>
                </asp:TemplateField>             
                <asp:TemplateField HeaderText="<%$ Resources:labels, email %>">
                  <ItemTemplate>
                    <%# Server.HtmlEncode(Eval("email").ToString()) %>
                  </ItemTemplate>
                  <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtEmail" Text='<%# Eval("email") %>' />
                  </EditItemTemplate>
                </asp:TemplateField>
                
               
            </Columns>
            <HeaderStyle HorizontalAlign="Left" />
        </asp:GridView>

Having a problem with the users.aspx.cs so I started over, could you tell me what to delete, I also renamed these so I can still use the originals also.
public partial class admin_newuser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
gridUsers.RowEditing += new GridViewEditEventHandler(grid_RowEditing);
gridUsers.RowUpdating += new GridViewUpdateEventHandler(grid_RowUpdating);
gridUsers.RowCancelingEdit += delegate { Response.Redirect(Request.RawUrl); };
gridUsers.RowDeleting += new GridViewDeleteEventHandler(grid_RowDeleting);
gridUsers.RowDataBound += new GridViewRowEventHandler(gridUsers_RowDataBound);

CreateUserWizard1.CreatingUser += new LoginCancelEventHandler(CreateUserWizard1_CreatingUser);
CreateUserWizard1.CreatedUser += new EventHandler(CreateUserWizard1_CreatedUser);

Page.Title = Resources.labels.users;

if (!Page.IsPostBack)
BindGrid();
}

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[1] 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 void BindGrid()
{
int count = 0;
gridUsers.DataSource = Membership.Provider.GetAllUsers(0, 999, out count);
gridUsers.DataKeyNames = new string[] { "username" };
gridUsers.DataBind();
if (count > 0)
gridUsers.HeaderRow.Cells[1].Text = Resources.labels.userName;
}

/// <summary>
/// Handles the RowDeleting event of the grid control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewDeleteEventArgs"/> instance containing the event data.</param>
void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string username = (string)gridUsers.DataKeys[e.RowIndex].Value;
string[] roles = Roles.GetRolesForUser(username);
Membership.DeleteUser(username);

if (roles.Length > 0)
Roles.RemoveUserFromRoles(username, roles);

AuthorProfile profile = AuthorProfile.GetProfile(username);
if (profile != null)
profile.Delete();

if (HttpContext.Current.User.Identity.Name.Equals(username, StringComparison.OrdinalIgnoreCase))
FormsAuthentication.SignOut();

Response.Redirect(Request.RawUrl);
}

/// <summary>
/// Handles the RowUpdating event of the grid control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param>
void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string username = (string)gridUsers.DataKeys[e.RowIndex].Value;
//TextBox txtPassword = (TextBox)gridUsers.Rows[e.RowIndex].FindControl("txtPassword");
TextBox txtEmail = (TextBox)gridUsers.Rows[e.RowIndex].FindControl("txtEmail");
//TextBox txtUsername = (TextBox)gridUsers.Rows[e.RowIndex].FindControl("txtUsername");

MembershipUser oldUser = Membership.GetUser(username);
//string[] oldRoles = Roles.GetRolesForUser(username);
//if (oldRoles.Length > 0)
//    Roles.RemoveUserFromRoles(username, oldRoles);
//Membership.DeleteUser(username);

//MembershipUser user = Membership.CreateUser(txtUsername.Text, txtPassword.Text, txtEmail.Text);
//if (oldRoles.Length > 0)
//    Roles.AddUserToRoles(user.UserName, oldRoles);

//if (username != user.UserName)
//    UpdatePosts(username, txtUsername.Text);

oldUser.Email = txtEmail.Text;
Membership.UpdateUser(oldUser);

if (HttpContext.Current.User.Identity.Name.Equals(oldUser.UserName, StringComparison.CurrentCultureIgnoreCase))
FormsAuthentication.SignOut();

Response.Redirect(Request.RawUrl);
}

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 RowEditing event of the grid control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewEditEventArgs"/> instance containing the event data.</param>
void grid_RowEditing(object sender, GridViewEditEventArgs e)
{
gridUsers.EditIndex = e.NewEditIndex;
BindGrid();
}

/// <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("Users.aspx", true);
}


/// <summary>
/// Handles the CheckedChanged event of the cb 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>
protected void cb_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
GridViewRow drv = (GridViewRow)cb.Parent.BindingContainer;
string _userName = gridUsers.DataKeys[drv.DataItemIndex].Value.ToString();
string _roleToUse = cb.Text;
if (cb.Checked == false)
{
if (User.Identity.Name != _userName.ToLower())
Roles.RemoveUserFromRole(_userName, _roleToUse);
}
else
{
if (!Roles.IsUserInRole(_userName, _roleToUse))
Roles.AddUserToRole(_userName, _roleToUse);
}
Response.Redirect("Users.aspx", true);
}

/// <summary>
/// Handles the Load event of the gridUsers 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>
protected void gridUsers_Load(object sender, EventArgs e)
{
for (int i = 0; i < gridUsers.Rows.Count; i++)
{
string[] allRoles = Roles.GetAllRoles();
foreach (string _role in allRoles)
{
CheckBox cb = new CheckBox();
cb.Text = _role;
cb.Checked = Roles.IsUserInRole(gridUsers.DataKeys[i].Value.ToString(), _role);
cb.AutoPostBack = true;
cb.TextAlign = TextAlign.Right;
cb.Style.Add("display", "inline");
cb.Style.Add("padding-right", "15px");
cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
gridUsers.Rows[i].Cells[4].Controls.Add(cb);
}
}
}
}

Coordinator
Mar 27, 2009 at 6:08 AM
In users.aspx, you still need to remove OnLoad="gridUsers_Load".

To get rid of the Edit and Delete buttons, just remove

AutoGenerateEditButton="true"
 ... and ...
AutoGenerateDeleteButton="true"

In users.aspx.cs, since you've removed the Edit and Delete buttons above, you can eliminate these lines:

gridUsers.RowEditing += new GridViewEditEventHandler(grid_RowEditing);
gridUsers.RowUpdating += new GridViewUpdateEventHandler(grid_RowUpdating);
gridUsers.RowCancelingEdit += delegate { Response.Redirect(Request.RawUrl); };
gridUsers.RowDeleting += new GridViewDeleteEventHandler(grid_RowDeleting);
gridUsers.RowDataBound += new GridViewRowEventHandler(gridUsers_RowDataBound);

You can also delete the following event handlers in users.aspx.cs:

gridUsers_RowDataBound
grid_RowDeleting()
grid_RowUpdating()
UpdatePosts()
grid_RowEditing()
cb_CheckedChanged()
gridUsers_Load()

That should do it.  There won't be much left!