2 // System.Web.UI.WebControls.MultiView.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2004 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
34 using System.Globalization;
37 using System.ComponentModel;
39 namespace System.Web.UI.WebControls
41 // [ControlBuilder (typeof(MultiViewControlBuilder)]
42 [Designer ("System.Web.UI.Design.WebControls.MultiViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [ToolboxData ("<{0}:MultiView runat=\"server\"></{0}:MultiView>")]
44 [ParseChildren (false, ChildControlType = typeof(View))]
45 [DefaultEvent ("ActiveViewChanged")]
46 public class MultiView: Control
48 public static readonly string NextViewCommandName = "NextView";
49 public static readonly string PreviousViewCommandName = "PrevView";
50 public static readonly string SwitchViewByIDCommandName = "SwitchViewByID";
51 public static readonly string SwitchViewByIndexCommandName = "SwitchViewByIndex";
53 private static readonly object ActiveViewChangedEvent = new object();
56 int initialIndex = -1;
59 public event EventHandler ActiveViewChanged {
60 add { Events.AddHandler (ActiveViewChangedEvent, value); }
61 remove { Events.RemoveHandler (ActiveViewChangedEvent, value); }
64 protected override void AddParsedSubObject (object ob)
67 Controls.Add (ob as View);
70 protected override ControlCollection CreateControlCollection ()
72 return new ViewCollection (this);
75 public View GetActiveView ()
77 if (viewIndex < 0 || viewIndex >= Controls.Count)
78 throw new HttpException ("The ActiveViewIndex is not set to a valid View control");
79 return Controls [viewIndex] as View;
82 public void SetActiveView (View view)
84 int i = Controls.IndexOf (view);
86 throw new HttpException ("The provided view is not contained in the MultiView control.");
92 public virtual int ActiveViewIndex {
93 get { return viewIndex; }
100 if (value < -1 || value >= Controls.Count)
101 throw new ArgumentOutOfRangeException ();
104 ((View)Controls [viewIndex]).NotifyActivation (false);
109 ((View)Controls [viewIndex]).NotifyActivation (true);
111 UpdateViewVisibility ();
117 public virtual new bool EnableTheming
120 throw new NotImplementedException ();
123 throw new NotImplementedException ();
127 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
129 public virtual ViewCollection Views {
130 get { return Controls as ViewCollection; }
133 protected override bool OnBubbleEvent (object source, EventArgs e)
135 CommandEventArgs ca = e as CommandEventArgs;
137 switch (ca.CommandName) {
139 if (viewIndex < Controls.Count - 1 && Controls.Count > 0)
140 ActiveViewIndex = viewIndex + 1;
145 ActiveViewIndex = viewIndex - 1;
148 case "SwitchViewByID":
149 foreach (View v in Controls)
150 if (v.ID == (string)ca.CommandArgument) {
156 case "SwitchViewByIndex":
157 int i = (int) Convert.ChangeType (ca.CommandArgument, typeof(int));
165 protected internal override void OnInit (EventArgs e)
168 Page.RegisterRequiresControlState (this);
169 if (initialIndex != -1) {
170 ActiveViewIndex = initialIndex;
176 void UpdateViewVisibility ()
178 for (int n=0; n<Views.Count; n++)
179 Views [n].Visible = (n == viewIndex);
182 protected internal override void RemovedControl (Control ctl)
184 if (viewIndex >= Controls.Count) {
185 viewIndex = Controls.Count - 1;
186 UpdateViewVisibility ();
189 base.RemovedControl (ctl);
192 protected internal override void LoadControlState (object state)
195 viewIndex = (int)state;
196 UpdateViewVisibility ();
201 protected internal override object SaveControlState ()
203 if (viewIndex != -1) return viewIndex;
207 protected virtual void OnActiveViewChanged (EventArgs e)
209 if (Events != null) {
210 EventHandler eh = (EventHandler) Events [ActiveViewChangedEvent];
211 if (eh != null) eh (this, e);
215 protected internal override void Render (HtmlTextWriter writer)
217 if (!initied) viewIndex = initialIndex;
219 GetActiveView ().Render (writer);