2010-03-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Web / System.Web.UI.WebControls / DataKey.cs
blob3b8429e64726e4e8b68da34839a94e29aea7437d
1 //
2 // System.Web.UI.WebControls.DataKey.cs
3 //
4 // Authors:
5 // Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #if NET_2_0
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
36 namespace System.Web.UI.WebControls
38 public class DataKey : IStateManager
40 IOrderedDictionary keyTable;
41 string[] keyNames;
42 bool trackViewState;
43 IOrderedDictionary readonlyKeyTable;
45 public DataKey (IOrderedDictionary keyTable)
47 this.keyTable = keyTable;
50 public DataKey (IOrderedDictionary keyTable, string[] keyNames)
52 this.keyTable = keyTable;
53 this.keyNames = keyNames;
56 public virtual object this [int index] {
57 get { return keyTable [index]; }
60 public virtual object this [string name] {
61 get { return keyTable [name]; }
64 public virtual object Value {
65 get {
66 if (keyTable.Count == 0)
67 return null;
68 return keyTable [0];
72 public virtual IOrderedDictionary Values {
73 get {
74 if (readonlyKeyTable == null) {
75 if (keyTable is OrderedDictionary)
76 readonlyKeyTable = ((OrderedDictionary)keyTable).AsReadOnly ();
77 else
78 readonlyKeyTable = keyTable;
80 return readonlyKeyTable;
84 protected virtual void LoadViewState (object savedState)
86 if (savedState is Pair) {
87 Pair p = (Pair) savedState;
88 object[] akeys = (object[]) p.First;
89 object[] avals = (object[]) p.Second;
90 for (int n=0; n<akeys.Length; n++) {
91 keyTable [akeys[n]] = avals [n];
93 } else if (savedState is object[]) {
94 object[] avals = (object[]) savedState;
95 for (int n=0; n<avals.Length; n++)
96 keyTable [keyNames[n]] = avals [n];
100 protected virtual object SaveViewState ()
102 if (keyTable.Count == 0) return null;
104 if (keyNames != null) {
105 object[] avals = new object [keyTable.Count];
106 int n=0;
107 foreach (object val in keyTable.Values)
108 avals [n++] = val;
109 return avals;
110 } else {
111 object[] avals = new object [keyTable.Count];
112 object[] akeys = new object [keyTable.Count];
113 int n=0;
114 foreach (DictionaryEntry de in keyTable) {
115 akeys [n] = de.Key;
116 avals [n++] = de.Value;
118 return new Pair (akeys, avals);
122 protected virtual void TrackViewState ()
124 trackViewState = true;
127 protected virtual bool IsTrackingViewState {
128 get { return trackViewState; }
131 void IStateManager.LoadViewState (object savedState)
133 LoadViewState (savedState);
136 object IStateManager.SaveViewState ()
138 return SaveViewState ();
141 void IStateManager.TrackViewState ()
143 TrackViewState ();
146 bool IStateManager.IsTrackingViewState {
147 get { return IsTrackingViewState; }
151 #endif