Page.RegisterRequiresViewStateEncryption Method ()


Registers a control with the page as one requiring view-state encryption.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Public Sub RegisterRequiresViewStateEncryption

Exception Condition

The RegisterRequiresViewStateEncryption method must be called before or during the page PreRenderphase in the page life cycle.

If you are developing a custom control that deals with potentially sensitive information, call the RegisterRequiresViewStateEncryption method to register the control with the page and ensure view state for the control is encrypted.

The entire page state will be encrypted if the ViewStateEncryptionMode is set to Auto or Always.

The following code example demonstrates setting the view-state encryption mode for a Page object and requesting encryption of the view state through RegisterRequiresViewStateEncryption. In this example, view state will be encrypted when customer information is retrieved from a database.

<%@ Page Language="VB" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">    
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If IsPostBack Then

            If (yesRetrieve.Checked) Then

                Dim conn As System.Data.SqlClient.SqlConnection = _
                  New System.Data.SqlClient.SqlConnection _
                  ("server=localhost;database=Northwind;Integrated Security=SSPI")
                Dim command As System.Data.SqlClient.SqlCommand = _
                command.CommandText = "Select [CustomerID] From [Customers]"
                Dim reader As System.Data.SqlClient.SqlDataReader = _
                customerid.Text = reader("CustomerID").ToString()
            End If
        End If
    End Sub

<html xmlns="" >
<head id="Head1" runat="server">
    <title>Customer Information</title>
    <form id="form1" runat="server">
        Customer identifier: 
        <asp:Label ID="customerid" runat="server" Text="Not available" />
        <br />
        Retrieve customer info: 
        <asp:RadioButton ID="yesRetrieve" Text="yes" runat="server" GroupName="group1" /> 
        <asp:RadioButton ID="noRetrieve" Text="no" runat="server" GroupName="group1" />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" />

.NET Framework
Available since 2.0
Return to top