MultiView.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // System.Web.UI.WebControls.MultiView.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.Globalization;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.ComponentModel;
  36. namespace System.Web.UI.WebControls
  37. {
  38. [ControlBuilder (typeof(MultiViewControlBuilder))]
  39. [Designer ("System.Web.UI.Design.WebControls.MultiViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. [ToolboxData ("<{0}:MultiView runat=\"server\"></{0}:MultiView>")]
  41. #if NET_2_0
  42. [ParseChildren (typeof(View))]
  43. #else
  44. [ParseChildren (false)]
  45. #endif
  46. [DefaultEvent ("ActiveViewChanged")]
  47. public class MultiView: Control
  48. {
  49. public static readonly string NextViewCommandName = "NextView";
  50. public static readonly string PreviousViewCommandName = "PrevView";
  51. public static readonly string SwitchViewByIDCommandName = "SwitchViewByID";
  52. public static readonly string SwitchViewByIndexCommandName = "SwitchViewByIndex";
  53. static readonly object ActiveViewChangedEvent = new object();
  54. int viewIndex = -1;
  55. int initialIndex = -1;
  56. public event EventHandler ActiveViewChanged {
  57. add { Events.AddHandler (ActiveViewChangedEvent, value); }
  58. remove { Events.RemoveHandler (ActiveViewChangedEvent, value); }
  59. }
  60. protected override void AddParsedSubObject (object ob)
  61. {
  62. if (ob is View)
  63. Controls.Add (ob as View);
  64. // LAMESPEC: msdn talks that only View contorls are allowed, for others controls HttpException should be thrown
  65. // but actually, aspx praser adds LiteralControl controls.
  66. //else
  67. // throw new HttpException ("MultiView cannot have children of type 'Control'. It can only have children of type View.");
  68. }
  69. protected override ControlCollection CreateControlCollection ()
  70. {
  71. return new ViewCollection (this);
  72. }
  73. public View GetActiveView ()
  74. {
  75. if (viewIndex < 0 || viewIndex >= Controls.Count)
  76. throw new HttpException ("The ActiveViewIndex is not set to a valid View control");
  77. return Controls [viewIndex] as View;
  78. }
  79. public void SetActiveView (View view)
  80. {
  81. int i = Controls.IndexOf (view);
  82. if (i == -1)
  83. throw new HttpException ("The provided view is not contained in the MultiView control.");
  84. ActiveViewIndex = i;
  85. }
  86. [DefaultValue (-1)]
  87. public virtual int ActiveViewIndex {
  88. get
  89. {
  90. if (Controls.Count == 0)
  91. return initialIndex;
  92. return viewIndex;
  93. }
  94. set
  95. {
  96. if (Controls.Count == 0) {
  97. initialIndex = value;
  98. return;
  99. }
  100. if (value < -1 || value >= Controls.Count)
  101. throw new ArgumentOutOfRangeException ();
  102. if (viewIndex != -1)
  103. ((View)Controls [viewIndex]).NotifyActivation (false);
  104. viewIndex = value;
  105. if (viewIndex != -1)
  106. ((View)Controls [viewIndex]).NotifyActivation (true);
  107. UpdateViewVisibility ();
  108. OnActiveViewChanged (EventArgs.Empty);
  109. }
  110. }
  111. [Browsable (true)]
  112. public virtual new bool EnableTheming
  113. {
  114. get { return base.EnableTheming; }
  115. set { base.EnableTheming = value; }
  116. }
  117. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  118. [Browsable (false)]
  119. public virtual ViewCollection Views {
  120. get { return Controls as ViewCollection; }
  121. }
  122. protected override bool OnBubbleEvent (object source, EventArgs e)
  123. {
  124. CommandEventArgs ca = e as CommandEventArgs;
  125. if (ca != null) {
  126. switch (ca.CommandName) {
  127. case "NextView":
  128. if (viewIndex < Controls.Count - 1 && Controls.Count > 0)
  129. ActiveViewIndex = viewIndex + 1;
  130. break;
  131. case "PrevView":
  132. if (viewIndex > 0)
  133. ActiveViewIndex = viewIndex - 1;
  134. break;
  135. case "SwitchViewByID":
  136. foreach (View v in Controls)
  137. if (v.ID == (string)ca.CommandArgument) {
  138. SetActiveView (v);
  139. break;
  140. }
  141. break;
  142. case "SwitchViewByIndex":
  143. int i = (int) Convert.ChangeType (ca.CommandArgument, typeof(int));
  144. ActiveViewIndex = i;
  145. break;
  146. }
  147. }
  148. return false;
  149. }
  150. protected internal override void OnInit (EventArgs e)
  151. {
  152. Page.RegisterRequiresControlState (this);
  153. if (initialIndex != -1) {
  154. ActiveViewIndex = initialIndex;
  155. initialIndex = -1;
  156. }
  157. base.OnInit (e);
  158. }
  159. void UpdateViewVisibility ()
  160. {
  161. for (int n=0; n<Views.Count; n++)
  162. Views [n].VisibleInternal = (n == viewIndex);
  163. }
  164. protected internal override void RemovedControl (Control ctl)
  165. {
  166. if (viewIndex >= Controls.Count) {
  167. viewIndex = Controls.Count - 1;
  168. UpdateViewVisibility ();
  169. }
  170. base.RemovedControl (ctl);
  171. }
  172. protected internal override void LoadControlState (object state)
  173. {
  174. if (state != null) {
  175. viewIndex = (int)state;
  176. UpdateViewVisibility ();
  177. }
  178. else viewIndex = -1;
  179. }
  180. protected internal override object SaveControlState ()
  181. {
  182. if (viewIndex != -1) return viewIndex;
  183. else return null;
  184. }
  185. protected virtual void OnActiveViewChanged (EventArgs e)
  186. {
  187. if (Events != null) {
  188. EventHandler eh = (EventHandler) Events [ActiveViewChangedEvent];
  189. if (eh != null) eh (this, e);
  190. }
  191. }
  192. protected internal override void Render (HtmlTextWriter writer)
  193. {
  194. if ((Controls.Count == 0) && (initialIndex != -1))
  195. viewIndex = initialIndex;
  196. if (viewIndex != -1)
  197. GetActiveView ().Render (writer);
  198. }
  199. }
  200. }
  201. #endif