Have a gridview that lists users who are locked out of the system. Admin staff can go on to this unlockuser page where the gridview will be displayed with the users various details, theres an unlock button on every row for each user. Just wondering what way to write the back end code for the gridview row command? So far I have

  MembershipUser usr = Membership.GetUser();
                usr.UnlockUser();

How do I get the user id into this? Do I need to declare it as a variable somewhere in the code behind?

Dani AI

Generated

Good direction so far — is right to put the user identifier on the row control, but a slightly cleaner and safer pattern is to store the identifier in the GridView DataKeys and use the RowCommand handler to look it up. That avoids fragile string parsing and keeps your template markup simpler. Two recommended flows follow: use the Membership API when you rely on ASP.NET membership, or update your own user table if you keep a separate IsLockedOut flag.

Example: unlock via the Membership provider (get the username or provider key from DataKeys, then call the provider API and rebind the grid):

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName != "Unlock") return;

    var btn = (Control)e.CommandSource;
    var row = (GridViewRow)btn.NamingContainer;
    var key = GridView1.DataKeys[row.RowIndex].Value;        // username or provider key

    var membershipUser = System.Web.Security.Membership.GetUser(key);
    if (membershipUser != null && membershipUser.IsLockedOut)
    {
        bool unlocked = membershipUser.UnlockUser();
        // optional: log result, show message if !unlocked
    }

    BindGrid(); // re-query data source and call GridView1.DataBind()
}

If you maintain a separate column in your DB, perform a parameterized update (avoid inline SQL concatenation), then rebind the grid:

using (var cn = new SqlConnection(connString))
using (var cmd = cn.CreateCommand())
{
    cmd.CommandText = "UPDATE Users SET IsLockedOut = @locked WHERE Id = @id";
    cmd.Parameters.AddWithValue("@locked", 0);
    cmd.Parameters.AddWithValue("@id", userId);
    cn.Open();
    cmd.ExecuteNonQuery();
}
BindGrid();

Practical notes: set GridView.DataKeyNames to the identifier you need (username or providerUserKey), protect the action with authorization checks and an audit log, avoid unlocking the current admin account by accident, and wrap DB/provider calls in try/catch. If you use an SqlDataSource or ObjectDataSource, call its Select/Bind method or GridView1.DataBind() after the change to refresh the list of locked users.

Recommended Answers

All 3 Replies

In your gridview itemtemplate button, you should add the commandArgument, like this:

<asp:button .... CommandArgument='<%#Eval("UserId") %>' />

Then, in the BackEnd, on your RowCommand event, you can get the userId like this:

int UserId = int.Parse(e.CommandArgument.ToString())

Thats great, thank you. Any idea how I update the database table so that the isLockedOut field changes to false and then update the gridview to display the remaining locked out users?

I never used asp.net membership providers, so I can't help much with that.

But in plain SQL, it would be like this:

UPDATE [myUserTable] SET [myLockedField] = 0 WHERE [myUserIdField] = @UserId

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.