2 // System.Web.UI.WebControls.DataKey.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005-2010 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.
31 using System.Collections;
32 using System.Collections.Specialized;
34 namespace System.Web.UI.WebControls
36 public class DataKey : IStateManager
37 , IEquatable <DataKey>
39 IOrderedDictionary keyTable;
42 IOrderedDictionary readonlyKeyTable;
44 public DataKey (IOrderedDictionary keyTable)
45 : this (keyTable, null)
49 public DataKey (IOrderedDictionary keyTable, string[] keyNames)
51 this.keyTable = keyTable;
52 this.keyNames = keyNames;
55 public virtual object this [int index] {
56 get { return keyTable [index]; }
59 public virtual object this [string name] {
60 get { return keyTable [name]; }
63 public virtual object Value {
65 if (keyTable.Count == 0)
71 public virtual IOrderedDictionary Values {
73 if (readonlyKeyTable == null) {
74 if (keyTable is OrderedDictionary)
75 readonlyKeyTable = ((OrderedDictionary)keyTable).AsReadOnly ();
77 readonlyKeyTable = keyTable;
79 return readonlyKeyTable;
82 public bool Equals (DataKey other)
87 IOrderedDictionary otherKeyTable = other.keyTable;
88 if (keyTable != null && otherKeyTable != null) {
89 if (keyTable.Count != otherKeyTable.Count)
92 object thisValue, otherValue;
94 foreach (object key in keyTable.Keys) {
95 if (!otherKeyTable.Contains (key))
98 thisValue = keyTable [key];
99 otherValue = otherKeyTable [key];
101 if (thisValue == null ^ otherValue == null)
104 if (!thisValue.Equals (otherValue))
109 string[] otherKeyNames = other.keyNames;
110 if (keyNames != null && otherKeyNames != null) {
111 int len = keyNames.Length;
112 if (len != otherKeyNames.Length)
115 for (int i = 0; i < len; i++)
116 if (String.Compare (keyNames [i], otherKeyNames [i], StringComparison.Ordinal) != 0)
118 } else if (keyNames == null ^ otherKeyNames == null)
123 protected virtual void LoadViewState (object savedState)
125 if (savedState is Pair) {
126 Pair p = (Pair) savedState;
127 object[] akeys = (object[]) p.First;
128 object[] avals = (object[]) p.Second;
129 for (int n=0; n<akeys.Length; n++) {
130 keyTable [akeys[n]] = avals [n];
132 } else if (savedState is object[]) {
133 object[] avals = (object[]) savedState;
134 for (int n=0; n<avals.Length; n++)
135 keyTable [keyNames[n]] = avals [n];
139 protected virtual object SaveViewState ()
141 if (keyTable.Count == 0)
144 if (keyNames != null) {
145 object[] avals = new object [keyTable.Count];
147 foreach (object val in keyTable.Values)
151 object[] avals = new object [keyTable.Count];
152 object[] akeys = new object [keyTable.Count];
154 foreach (DictionaryEntry de in keyTable) {
156 avals [n++] = de.Value;
158 return new Pair (akeys, avals);
162 protected virtual void TrackViewState ()
164 trackViewState = true;
167 protected virtual bool IsTrackingViewState {
168 get { return trackViewState; }
171 void IStateManager.LoadViewState (object savedState)
173 LoadViewState (savedState);
176 object IStateManager.SaveViewState ()
178 return SaveViewState ();
181 void IStateManager.TrackViewState ()
186 bool IStateManager.IsTrackingViewState {
187 get { return IsTrackingViewState; }