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>")]
45 [ParseChildren (typeof(View))]
47 [ParseChildren (false)]
49 [DefaultEvent ("ActiveViewChanged")]
50 public class MultiView: Control
52 public static readonly string NextViewCommandName = "NextView";
53 public static readonly string PreviousViewCommandName = "PrevView";
54 public static readonly string SwitchViewByIDCommandName = "SwitchViewByID";
55 public static readonly string SwitchViewByIndexCommandName = "SwitchViewByIndex";
57 static readonly object ActiveViewChangedEvent = new object();
60 int initialIndex = -1;
62 public event EventHandler ActiveViewChanged {
63 add { Events.AddHandler (ActiveViewChangedEvent, value); }
64 remove { Events.RemoveHandler (ActiveViewChangedEvent, value); }
67 protected override void AddParsedSubObject (object ob)
70 Controls.Add (ob as View);
71 // LAMESPEC: msdn talks that only View contorls are allowed, for others controls HttpException should be thrown
72 // but actually, aspx praser adds LiteralControl controls.
74 // throw new HttpException ("MultiView cannot have children of type 'Control'. It can only have children of type View.");
77 protected override ControlCollection CreateControlCollection ()
79 return new ViewCollection (this);
82 public View GetActiveView ()
84 if (viewIndex < 0 || viewIndex >= Controls.Count)
85 throw new HttpException ("The ActiveViewIndex is not set to a valid View control");
86 return Controls [viewIndex] as View;
89 public void SetActiveView (View view)
91 int i = Controls.IndexOf (view);
93 throw new HttpException ("The provided view is not contained in the MultiView control.");
99 public virtual int ActiveViewIndex {
102 if (Controls.Count == 0)
109 if (Controls.Count == 0) {
110 initialIndex = value;
114 if (value < -1 || value >= Controls.Count)
115 throw new ArgumentOutOfRangeException ();
118 ((View)Controls [viewIndex]).NotifyActivation (false);
123 ((View)Controls [viewIndex]).NotifyActivation (true);
125 UpdateViewVisibility ();
127 OnActiveViewChanged (EventArgs.Empty);
132 public virtual new bool EnableTheming
134 get { return base.EnableTheming; }
135 set { base.EnableTheming = value; }
138 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
140 public virtual ViewCollection Views {
141 get { return Controls as ViewCollection; }
144 protected override bool OnBubbleEvent (object source, EventArgs e)
146 CommandEventArgs ca = e as CommandEventArgs;
148 switch (ca.CommandName) {
150 if (viewIndex < Controls.Count - 1 && Controls.Count > 0)
151 ActiveViewIndex = viewIndex + 1;
156 ActiveViewIndex = viewIndex - 1;
159 case "SwitchViewByID":
160 foreach (View v in Controls)
161 if (v.ID == (string)ca.CommandArgument) {
167 case "SwitchViewByIndex":
168 int i = (int) Convert.ChangeType (ca.CommandArgument, typeof(int));
176 protected internal override void OnInit (EventArgs e)
178 Page.RegisterRequiresControlState (this);
179 if (initialIndex != -1) {
180 ActiveViewIndex = initialIndex;
186 void UpdateViewVisibility ()
188 for (int n=0; n<Views.Count; n++)
189 Views [n].VisibleInternal = (n == viewIndex);
192 protected internal override void RemovedControl (Control ctl)
194 if (viewIndex >= Controls.Count) {
195 viewIndex = Controls.Count - 1;
196 UpdateViewVisibility ();
199 base.RemovedControl (ctl);
202 protected internal override void LoadControlState (object state)
205 viewIndex = (int)state;
206 UpdateViewVisibility ();
211 protected internal override object SaveControlState ()
213 if (viewIndex != -1) return viewIndex;
217 protected virtual void OnActiveViewChanged (EventArgs e)
219 if (Events != null) {
220 EventHandler eh = (EventHandler) Events [ActiveViewChangedEvent];
221 if (eh != null) eh (this, e);
225 protected internal override void Render (HtmlTextWriter writer)
227 if ((Controls.Count == 0) && (initialIndex != -1))
228 viewIndex = initialIndex;
230 GetActiveView ().Render (writer);