Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / MVC / Castle.MVC / Views / WebUserControlView.cs
blob08b51d5d4a14627c93e3f30a4f96dbe2d5c921df
1 #region Apache Notice
2 /*****************************************************************************
3 *
4 * Castle.MVC
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ********************************************************************************/
19 #endregion
21 #region Autors
23 /************************************************
24 * Gilles Bayon
25 *************************************************/
26 #endregion
28 #region Using
30 using System;
31 using System.Reflection;
32 using System.Text;
33 using System.Web.UI;
34 using System.Web.UI.WebControls;
35 using Castle.MVC.Configuration;
36 using Castle.MVC.Controllers;
37 using Castle.MVC.StatePersister;
38 using Castle.MVC.States;
39 using Castle.Windsor;
41 #endregion
44 namespace Castle.MVC.Views
46 /// <summary>
47 /// Represent a Web UserControl, give you access to the current state and page controller.
48 /// </summary>
49 public class WebUserControlView : UserControl
52 #region Fields
54 private IState _state = null;
56 #endregion
58 #region Properties
60 /// <summary>
61 /// Gets the user process state.
62 /// </summary>
63 public IState State
65 get { return _state; }
69 #endregion
71 #region Constructor
73 /// <summary>
74 /// Default cosntructor
75 /// </summary>
76 public WebUserControlView():base()
78 this.Init +=new EventHandler(WebUserControlView_Init);
79 this.Load +=new System.EventHandler(WebUserControlView_Load);
81 #endregion
83 #region Methods
85 private void WebUserControlView_Init(object sender, EventArgs e)
87 IWindsorContainer container = ContainerWebAccessorUtil.ObtainContainer();
89 // Get the State
90 IStatePersister statePersister = (IStatePersister) container[typeof(IStatePersister)];
91 _state = statePersister.Load();
92 // Acquire current view
93 _state.CurrentView = ConfigUtil.Settings.GetView(this.Request.Path);
94 _state.Save();
96 ControllerTree tree = (ControllerTree) container["mvc.controllerTree"];
97 PropertyControllerCollection propertiesController = tree.GetControllers( this.GetType().BaseType );
99 if (propertiesController!=null)
101 for(int i=0; i<propertiesController.Count; i++)
103 IController controller = container[propertiesController[i].ControllerType ] as IController;
104 propertiesController[i].PropertyInfo.SetValue(this, controller, null);
109 private void WebUserControlView_Load(object sender, EventArgs e)
111 // Try to set the command name on the state object
112 Control control = null;
113 bool findControl = false;
114 foreach(string controllName in this.Request.Form)
116 control = this.FindControl(controllName);
117 if (control is IPostBackEventHandler)
119 findControl =true;
120 break;
123 // Another try
124 if (!findControl && this.Request.Form["__EVENTTARGET"]!=null)
126 control = this.FindControl(this.Request.Form["__EVENTTARGET"].Replace(this.UniqueID+ ":", ""));
129 // The Control.ViewState property is associated with each server control
130 // in your web form
131 // The commandName is in the control.ViewState["CommandName"]
132 // wich is protected :-(
133 if (control!=null)
135 PropertyInfo propertyInfo = typeof(Control).GetProperty("ViewState",BindingFlags.Instance
136 | BindingFlags.IgnoreReturn
137 | BindingFlags.Public
138 | BindingFlags.NonPublic);
139 StateBag statebag = propertyInfo.GetValue(control,null) as StateBag;
141 _state.Command = statebag["CommandName"] as string;
143 _state.Save();
146 /// <summary>
147 /// Set the focus on a control
148 /// </summary>
149 /// <param name="clientID">The client id of the control.</param>
150 public void SetFocus(string clientID )
152 StringBuilder stringBuilder = new StringBuilder();
154 stringBuilder.Append("<script language='javascript'>");
155 stringBuilder.Append(" document.getElementById('" + clientID + "').focus()");
156 stringBuilder.Append("</script>");
157 this.Page.RegisterStartupScript("Onload", stringBuilder.ToString());
160 /// <summary>
161 /// Set the focus on a control
162 /// </summary>
163 /// <param name="control">The control</param>
164 public void SetFocus(WebControl control )
166 StringBuilder stringBuilder = new StringBuilder();
168 stringBuilder.Append("<script language='javascript'>");
169 stringBuilder.Append(" document.getElementById('" + control.ClientID + "').focus()");
170 stringBuilder.Append("</script>");
171 this.Page.RegisterStartupScript("Onload", stringBuilder.ToString());
174 #endregion