Applying patch from Jonathon Rossi
[castle.git] / Experiments / MVC / Castle.MVC / Views / WebFormView.cs
blob03a3e1f943abf69afec9083e909a31361b3c4011
2 #region Apache Notice
3 /*****************************************************************************
4 *
5 * Castle.MVC
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ********************************************************************************/
20 #endregion
22 #region Autors
24 /************************************************
25 * Gilles Bayon
26 *************************************************/
27 #endregion
29 #region Using
31 using System;
32 using System.Reflection;
33 using System.Text;
34 using System.Web.UI;
35 using System.Web.UI.WebControls;
36 using Castle.MVC.Configuration;
37 using Castle.MVC.Controllers;
38 using Castle.MVC.StatePersister;
39 using Castle.MVC.States;
40 using Castle.Windsor;
42 #endregion
44 namespace Castle.MVC.Views
46 /// <summary>
47 /// Base class for user interaction in Web application. You can inherit from
48 /// this class when developing your Web forms.
49 /// </summary>
50 public class WebFormView : Page, IView
53 #region Fields
55 private IState _state = null;
57 #endregion
59 #region Constructor
61 /// <summary>
62 /// Default cosntructor
63 /// </summary>
64 public WebFormView():base()
66 this.Init += new EventHandler(WebFormView_Init);
67 this.Load += new EventHandler(WebFormView_Load);
69 #endregion
71 #region IView members
73 /// <summary>
74 /// Gets the current view name.
75 /// </summary>
76 public string View
78 get { return _state.CurrentView; }
81 /// <summary>
82 /// Gets access to the user process state.
83 /// </summary>
84 public IState State
86 get { return _state; }
88 #endregion
90 #region Methods
92 private void WebFormView_Init(object sender, EventArgs e)
94 IWindsorContainer container = ContainerWebAccessorUtil.ObtainContainer();
96 // Get the State
97 IStatePersister statePersister = (IStatePersister) container[typeof(IStatePersister)];
98 _state = statePersister.Load();
99 // Acquire current view
100 _state.CurrentView = ConfigUtil.Settings.GetView(this.Request.Path);
101 _state.Save();
103 ControllerTree tree = (ControllerTree) container["mvc.controllerTree"];
104 PropertyControllerCollection propertiesController = tree.GetControllers( this.GetType().BaseType );
106 if (propertiesController!=null)
108 for(int i=0; i<propertiesController.Count; i++)
110 IController controller = container[propertiesController[i].ControllerType ] as IController;
111 propertiesController[i].PropertyInfo.SetValue(this, controller, null);
116 private void WebFormView_Load(object sender, EventArgs e)
118 // Try to set the command name on the state object
119 Control control = null;
120 bool findControl = false;
121 foreach(string controllName in this.Request.Form)
123 control = this.FindControl(controllName);
124 if (control is IPostBackEventHandler)
126 findControl =true;
127 break;
130 // Another try
131 if (!findControl && this.Request.Form["__EVENTTARGET"]!=null)
133 control = this.FindControl(this.Request.Form["__EVENTTARGET"].Replace(this.UniqueID+ ":", ""));
136 // The Control.ViewState property is associated with each server control
137 // in your web form
138 // The commandName is in the control.ViewState["CommandName"]
139 // wich is protected :-(
140 if (control!=null)
142 PropertyInfo propertyInfo = typeof(Control).GetProperty("ViewState",BindingFlags.Instance
143 | BindingFlags.IgnoreReturn
144 | BindingFlags.Public
145 | BindingFlags.NonPublic);
146 StateBag statebag = propertyInfo.GetValue(control,null) as StateBag;
148 _state.Command = statebag["CommandName"] as string;
150 else
152 _state.Command = string.Empty;
154 _state.Save();
158 /// <summary>
159 /// Set the focus on a control
160 /// </summary>
161 /// <param name="clientID">The client id of the control.</param>
162 public void SetFocus(string clientID )
164 StringBuilder stringBuilder = new StringBuilder();
166 stringBuilder.Append("<script language='javascript'>");
167 stringBuilder.Append(" document.getElementById('" + clientID + "').focus()");
168 stringBuilder.Append("</script>");
169 this.RegisterStartupScript("Onload", stringBuilder.ToString());
172 /// <summary>
173 /// Set the focus on a control
174 /// </summary>
175 /// <param name="control">The control</param>
176 public void SetFocus(WebControl control )
178 StringBuilder stringBuilder = new StringBuilder();
180 stringBuilder.Append("<script language='javascript'>");
181 stringBuilder.Append(" document.getElementById('" + control.ClientID + "').focus()");
182 stringBuilder.Append("</script>");
183 this.RegisterStartupScript("Onload", stringBuilder.ToString());
186 /// <summary>
187 /// ToString Override
188 /// </summary>
189 /// <returns></returns>
190 public override string ToString()
192 return _state.GetType().FullName + ":" + _state.CurrentView;
194 #endregion