2 // System.Web.UI.WebControls.DataKey.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Collections.Specialized;
36 namespace System.Web.UI.WebControls
38 public class DataKey : IStateManager
40 IOrderedDictionary keyTable;
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 { return keyTable [0]; }
68 public virtual IOrderedDictionary Values {
70 if (readonlyKeyTable == null) {
71 if (keyTable is OrderedDictionary)
72 readonlyKeyTable = ((OrderedDictionary)keyTable).AsReadOnly ();
74 readonlyKeyTable = keyTable;
76 return readonlyKeyTable;
80 protected virtual void LoadViewState (object savedState)
82 if (savedState is Pair) {
83 Pair p = (Pair) savedState;
84 object[] akeys = (object[]) p.First;
85 object[] avals = (object[]) p.Second;
86 for (int n=0; n<akeys.Length; n++) {
87 keyTable [akeys[n]] = avals [n];
89 } else if (savedState is object[]) {
90 object[] avals = (object[]) savedState;
91 for (int n=0; n<avals.Length; n++)
92 keyTable [keyNames[n]] = avals [n];
96 protected virtual object SaveViewState ()
98 if (keyTable.Count == 0) return null;
100 if (keyNames != null) {
101 object[] avals = new object [keyTable.Count];
103 foreach (object val in keyTable.Values)
107 object[] avals = new object [keyTable.Count];
108 object[] akeys = new object [keyTable.Count];
110 foreach (DictionaryEntry de in keyTable) {
112 avals [n++] = de.Value;
114 return new Pair (akeys, avals);
118 void IStateManager.LoadViewState (object savedState)
120 LoadViewState (savedState);
123 object IStateManager.SaveViewState ()
125 return SaveViewState ();
128 void IStateManager.TrackViewState ()
130 trackViewState = true;
133 bool IStateManager.IsTrackingViewState {
134 get { return trackViewState; }