3 /*****************************************************************************
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 ********************************************************************************/
24 /************************************************
26 *************************************************/
32 using System
.Reflection
;
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
;
44 namespace Castle
.MVC
.Views
47 /// Base class for user interaction in Web application. You can inherit from
48 /// this class when developing your Web forms.
50 public class WebFormView
: Page
, IView
55 private IState _state
= null;
62 /// Default cosntructor
64 public WebFormView():base()
66 this.Init
+= new EventHandler(WebFormView_Init
);
67 this.Load
+= new EventHandler(WebFormView_Load
);
74 /// Gets the current view name.
78 get { return _state.CurrentView; }
82 /// Gets access to the user process state.
86 get { return _state; }
92 private void WebFormView_Init(object sender
, EventArgs e
)
94 IWindsorContainer container
= ContainerWebAccessorUtil
.ObtainContainer();
97 IStatePersister statePersister
= (IStatePersister
) container
[typeof(IStatePersister
)];
98 _state
= statePersister
.Load();
99 // Acquire current view
100 _state
.CurrentView
= ConfigUtil
.Settings
.GetView(this.Request
.Path
);
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
)
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
138 // The commandName is in the control.ViewState["CommandName"]
139 // wich is protected :-(
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;
152 _state
.Command
= string.Empty
;
159 /// Set the focus on a control
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());
173 /// Set the focus on a control
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());
187 /// ToString Override
189 /// <returns></returns>
190 public override string ToString()
192 return _state
.GetType().FullName
+ ":" + _state
.CurrentView
;