Share via


IPersonalizable.Save(PersonalizationDictionary) 메서드

정의

사용자 지정 속성과 내부 상태 정보를 컨트롤의 PersonalizationDictionary 개체에 저장합니다.

public:
 void Save(System::Web::UI::WebControls::WebParts::PersonalizationDictionary ^ state);
public void Save (System.Web.UI.WebControls.WebParts.PersonalizationDictionary state);
abstract member Save : System.Web.UI.WebControls.WebParts.PersonalizationDictionary -> unit
Public Sub Save (state As PersonalizationDictionary)

매개 변수

state
PersonalizationDictionary

내부 데이터 저장소에서 로드된 사용자 지정 범위의 데이터를 포함하는 PersonalizationDictionary입니다.

예제

다음 코드 예제를 구현 하는 방법을 보여 줍니다 합니다 Save 메서드는 사용자 지정에서 WebPart 제어 합니다. 이 예제를 실행 하는 데 필요한 전체 코드의 예제 섹션을 참조 하세요.를 IPersonalizable 클래스 개요입니다.

namespace Samples.AspNet.CS.Controls
{

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Web;
    using System.Web.UI;
    using System.Security.Permissions;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    public class UrlListWebPart : WebPart, IPersonalizable
    {
        private ArrayList _sharedUrls;
        private ArrayList _userUrls;
        private bool _listDirty;

        private TextBox _nameTextBox;
        private TextBox _urlTextBox;
        private Button _addButton;
        private BulletedList _list;

        protected override void CreateChildControls()
        {
            Label nameLabel = new Label();
            Label urlLabel = new Label();
            LiteralControl breakLiteral1 = new LiteralControl("<br />");
            LiteralControl breakLiteral2 = new LiteralControl("<br />");
            LiteralControl breakLiteral3 = new LiteralControl("<br />");

            _nameTextBox = new TextBox();
            _urlTextBox = new TextBox();
            _addButton = new Button();
            _list = new BulletedList();

            nameLabel.Text = "Name: ";
            urlLabel.Text = "URL: ";
            _nameTextBox.ID = "nameTextBox";
            _urlTextBox.ID = "urlTextBox";
            _addButton.Text = "Add";
            _addButton.ID = "addButton";
            _addButton.Click += new EventHandler(this.OnClickAddButton);
            _list.DisplayMode = BulletedListDisplayMode.HyperLink;
            _list.ID = "list";

            Controls.Add(nameLabel);
            Controls.Add(_nameTextBox);
            Controls.Add(breakLiteral1);

            Controls.Add(urlLabel);
            Controls.Add(_urlTextBox);
            Controls.Add(breakLiteral2);

            Controls.Add(_addButton);
            Controls.Add(breakLiteral3);

            Controls.Add(_list);
        }

        private void OnClickAddButton(object sender, EventArgs e)
        {
            string name = _nameTextBox.Text.Trim();
            string url = _urlTextBox.Text.Trim();

            Pair p = new Pair(name, url);
            if (WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
            {
                _sharedUrls ??= new ArrayList();
                _sharedUrls.Add(p);
            }
            else
            {
                _userUrls ??= new ArrayList();
                _userUrls.Add(p);
            }

            OnUrlAdded();
        }

        protected virtual void OnUrlAdded()
        {
            _listDirty = true;
            ChildControlsCreated = false;
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (_sharedUrls != null)
            {
                foreach (Pair p in _sharedUrls)
                {
                    _list.Items.Add(new ListItem((string)p.First, (string)p.Second));
                }
            }
            if (_userUrls != null)
            {
                foreach (Pair p in _userUrls)
                {
                    _list.Items.Add(new ListItem((string)p.First, (string)p.Second));
                }
            }

            base.RenderContents(writer);
        }

        public virtual bool IsDirty
        {
            get
            {
                return _listDirty;
            }
        }
        public new virtual void Load(PersonalizationDictionary state)
        {
            if (state != null)
            {
                PersonalizationEntry sharedUrlsEntry = state["sharedUrls"];
                if (sharedUrlsEntry != null)
                {
                    _sharedUrls = (ArrayList)sharedUrlsEntry.Value;
                }

                PersonalizationEntry userUrlsEntry = state["userUrls"];
                if (userUrlsEntry != null)
                {
                    _userUrls = (ArrayList)userUrlsEntry.Value;
                }
            }
        }

        public virtual void Save(PersonalizationDictionary state)
        {
            if ((_sharedUrls != null) && (_sharedUrls.Count != 0))
            {
                state["sharedUrls"] = new PersonalizationEntry(_sharedUrls, PersonalizationScope.Shared);
            }
            if ((_userUrls != null) && (_userUrls.Count != 0))
            {
                state["userUrls"] = new PersonalizationEntry(_userUrls, PersonalizationScope.User);
            }
        }
    }
}

Imports System.Collections
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Drawing
Imports System.Web
Imports System.Web.UI
Imports System.Security.Permissions
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class UrlListWebPart
      Inherits WebPart
      Implements IPersonalizable
      Private _sharedUrls As ArrayList
      Private _userUrls As ArrayList
      Private _listDirty As Boolean

      Private _nameTextBox As TextBox
      Private _urlTextBox As TextBox
      Private _addButton As Button
      Private _list As BulletedList


      Protected Overrides Sub CreateChildControls()
          Dim nameLabel As New Label()
          Dim urlLabel As New Label()
          Dim breakLiteral1 As New LiteralControl("<br />")
          Dim breakLiteral2 As New LiteralControl("<br />")
          Dim breakLiteral3 As New LiteralControl("<br />")

          _nameTextBox = New TextBox()
          _urlTextBox = New TextBox()
          _addButton = New Button()
          _list = New BulletedList()

          nameLabel.Text = "Name: "
          urlLabel.Text = "URL: "
          _nameTextBox.ID = "nameTextBox"
          _urlTextBox.ID = "urlTextBox"
          _addButton.Text = "Add"
          _addButton.ID = "addButton"
          AddHandler _addButton.Click, AddressOf Me.OnClickAddButton
          _list.DisplayMode = BulletedListDisplayMode.HyperLink
          _list.ID = "list"

          Controls.Add(nameLabel)
          Controls.Add(_nameTextBox)
          Controls.Add(breakLiteral1)

          Controls.Add(urlLabel)
          Controls.Add(_urlTextBox)
          Controls.Add(breakLiteral2)

          Controls.Add(_addButton)
          Controls.Add(breakLiteral3)

          Controls.Add(_list)

      End Sub


      Private Sub OnClickAddButton(ByVal sender As Object, ByVal e As EventArgs)
          Dim name As String = _nameTextBox.Text.Trim()
          Dim url As String = _urlTextBox.Text.Trim()

          Dim p As New Pair(name, url)
          If WebPartManager.Personalization.Scope = PersonalizationScope.Shared Then
              If _sharedUrls Is Nothing Then
                  _sharedUrls = New ArrayList()
              End If
              _sharedUrls.Add(p)
          Else
              If _userUrls Is Nothing Then
                  _userUrls = New ArrayList()
              End If
              _userUrls.Add(p)
          End If

          OnUrlAdded()

      End Sub


      Protected Overridable Sub OnUrlAdded()
          _listDirty = True
          ChildControlsCreated = False

      End Sub


      Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
          If Not (_sharedUrls Is Nothing) Then
              Dim p As Pair
              For Each p In _sharedUrls
                  _list.Items.Add(New ListItem(CStr(p.First), CStr(p.Second)))
              Next p
          End If
          If Not (_userUrls Is Nothing) Then
              Dim p As Pair
              For Each p In _userUrls
                  _list.Items.Add(New ListItem(CStr(p.First), CStr(p.Second)))
              Next p
          End If

          MyBase.RenderContents(writer)

      End Sub


      Public Overridable ReadOnly Property IsDirty() As Boolean _
        Implements IPersonalizable.IsDirty
          Get
              Return _listDirty
          End Get
      End Property

      Public Overridable Shadows Sub Load(ByVal state As PersonalizationDictionary) _
        Implements IPersonalizable.Load
          If Not (state Is Nothing) Then
              Dim sharedUrlsEntry As PersonalizationEntry = state("sharedUrls")
              If Not (sharedUrlsEntry Is Nothing) Then
                  _sharedUrls = CType(sharedUrlsEntry.Value, ArrayList)
              End If

              Dim userUrlsEntry As PersonalizationEntry = state("userUrls")
              If Not (userUrlsEntry Is Nothing) Then
                  _userUrls = CType(userUrlsEntry.Value, ArrayList)
              End If
          End If

      End Sub

      Public Overridable Sub Save(ByVal state As PersonalizationDictionary) _
        Implements IPersonalizable.Save
          If Not (_sharedUrls Is Nothing) AndAlso _sharedUrls.Count <> 0 Then
              state("sharedUrls") = New PersonalizationEntry(_sharedUrls, PersonalizationScope.Shared)
          End If
          If Not (_userUrls Is Nothing) AndAlso _userUrls.Count <> 0 Then
              state("userUrls") = New PersonalizationEntry(_userUrls, PersonalizationScope.User)
          End If

      End Sub
  End Class


End Namespace

설명

서버 컨트롤 자체 사용자 지정 속성과 내부 상태 정보를 저장할 수는 PersonalizationDictionary 에 지정 된 된 state 매개 변수입니다. 정보를 일련의 이름/값 쌍으로 저장 됩니다. 이 통해 해당 컨트롤에 대 한 다음 호출에서 인식할 수 있는 이름/값 쌍을 사용 하 여 컨트롤의 경우는 Load 메서드.

컨트롤을 연결 된 참조할 수 있습니다 WebPartManager 제어 하 고 확인을 PersonalizationScope 현재 범위를 결정 하는 개체입니다. 사용자 지정 상태 정보는 현재 범위에 적절 해야 합니다. 각 PersonalizationEntry 에 추가 되는 값을 PersonalizationDictionary 병합할 때 개인 설정 인프라가이에 따라 달라 지므로 개체는 적절 한 범위 값을 사용 하 여 연결 해야 SharedUser-사용자 지정 데이터 범위 전달 하기 전에 Load 메서드.

웹 파트의 표준 ASP.NET 구현을 사용 하는 경우 컨트롤 ASP.NET에서 상태 사전에 저장 된 개체를 serialize 할 수 유지 해야 ObjectStateFormatter 클래스입니다. 실제로 다음으로 의미합니다.

  • 기본.NET Framework 형식, 문자열 및.NET Framework 형식 배열, 배열, 해시 테이블 목록과 하이브리드 사전 등 컬렉션 지향 자동으로 직렬화 됩니다.

  • 자체 제공 하는 사용자 지정 형식 TypeConverter 클래스를 직렬화 및 문자열에서 역직렬화 할 수 있는 직렬화 가능한 것으로 간주 됩니다.

  • 사용자 지정 형식으로 serialize 할 수 있는 BinaryFormatter 클래스 직렬화 가능한 것으로 간주 됩니다.

중요

하지 App_Code 디렉터리에 정의 된 클래스를 기반으로 하는 형식을 추가 해야 하며 그런 다음 기본 이진 serialization 메커니즘에 따라 달라 집니다. App_Code 기반 아티팩트가 하지 이진 직렬화 일관 되 게 때문에 시간에서 임의 지점에서 변경 어셈블리 이름을 가질 수 있습니다.

적용 대상