共用方式為


How to: Remove User Entries from the Personalization Store

In an ASP.NET Web application that uses personalization, there are some circumstances in which you might need to remove entries from the personalization store. How to: Enable Users to Clear Personalization State shows the simplest approach, which is to expose the ResetPersonalizationState method through a control on the page so that individual users can remove all personalization data that pertains to them. However, if you need to manage personalization data of other, multiple users, you must use the various methods of the PersonalizationAdministration class.

Note

Only administrators of the page should be granted access to the methods of the PersonalizationAdministration class.

To remove an individual user state from the personalization store

  1. In a part of the Web application that is available only to administrators, such as an ASP.NET page or a user control, create a set of controls to receive user input and send it to the server.

    The example in this procedure uses a TextBox control and a Button control.

  2. In the appropriate event handler, call the PersonalizationAdministration.ResetUserState method with the relative path to the Web application in question and the user name as parameters, as shown in the following example.

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Verify that the text box txtUser is not empty.
        If (txtUser.Text.Length < 1) Then
            Response.Write("You must enter a user name.")
        End If
        Return
    
        ' Reset the user.
        If (Not PersonalizationAdministration.ResetUserState("~/Default.aspx", txtUser.Text)) Then
            Response.Write("The user could not be found or the user has not personalized this page.")
        End If
    End Sub
    
    protected void btnClear_Click(object sender, EventArgs e)
    {
        // Verify that the text box is not empty.
        if (txtUser.Text.Length < 1)
        {
            Response.Write("You must enter a user name.");
            return;
        }
    
        // Reset the user.
        if (! PersonalizationAdministration.ResetUserState("~/Default.aspx", txtUser.Text))
        {
            Response.Write("The user could not be found or the user has not personalized this page");
        }
    }
    

To remove a group of users from the personalization store

  1. In a part of the Web application that is available only to administrators, create a set of controls to receive user input and send it to the server.

    The example in this procedure uses a TextBox control and a Button control.

  2. In the appropriate event handler, call the PersonalizationAdministration.ResetUserState method with the relative path to the Web application in question and a list of user names as parameters, as shown in the following example.

    Protected Sub btnClearList_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Verify that the text box is not empty.
        If (txtUser.Text.Length < 1) Then
            Response.Write("You must enter at least one user name.")
            Return
        End If
    
        ' Extract the list of users.
        Dim users As Array
        users = txtUserList.Text.Split(" ,;".ToCharArray())
    
        ' Reset the users.
        Dim RowsReset As Integer
        RowsReset = PersonalizationAdministration.ResetUserState("~/Default.aspx", users)
        Response.Write(RowsReset + "of " + users.Length + " users found and removed.")
    End Sub
    
    protected void btnClearList_Click(object sender, EventArgs e)
    {
        // Verify that the text box is not empty.
        if (txtUser.Text.Length < 1)
        {
            Response.Write("You must enter at least one user name.");
            return;
        }
    
        // Reset the users.
        string[] users = txtUserList.Text.Split(" ,;".ToCharArray());
        int RowsReset = PersonalizationAdministration.ResetUserState("~/Default.aspx", users);
        Response.Write(RowsReset + "of " + users.Length + " users found and removed.");
    }
    

To remove all inactive users from the personalization store

  1. In a part of the Web application that is only available to administrators, create a Calendar and a Button control.

  2. In the appropriate event handler, call the PersonalizationAdministration.ResetInactiveUserState method with the relative path to the Web application in question and a list of user names as parameters, as shown in the following example.

    Protected Sub btnClearInactive_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Verify that a date is selected.
        If (calInactive.SelectedDate = DateTime.MinValue) Then
            Response.Write("You must select a date.")
            Return
        End If
    
        ' Reset all users inactive since the selected date.
        Dim RowsReset As Integer
        RowsReset = PersonalizationAdministration.ResetInactiveUserState("~/Default.aspx", calInactive.SelectedDate)
        Response.Write(RowsReset + " inactive users removed.")
    End Sub
    
    protected void btnClearInactive_Click(object sender, EventArgs e)
    {
        // Verify that a date is selected.
        if (calInactive.SelectedDate == DateTime.MinValue)
        {
            Response.Write("You must select a date.");
            return;
        }
    
        // Reset all users inactive since the selected date.
        int RowsReset = PersonalizationAdministration.ResetInactiveUserState("~/Default.aspx", calInactive.SelectedDate);
        Response.Write(RowsReset + " inactive users removed.");
    }
    

See Also

Tasks

How to: Enable Users to Clear Personalization State

Reference

System.Web.UI.WebControls.WebParts

PersonalizationAdministration

Other Resources

ASP.NET Web Parts Controls