2 /*****************************************************************************
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
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 ********************************************************************************/
23 /************************************************
25 *************************************************/
31 using System
.Reflection
;
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
;
44 namespace Castle
.MVC
.Views
47 /// Represent a Web UserControl, give you access to the current state and page controller.
49 public class WebUserControlView
: UserControl
54 private IState _state
= null;
61 /// Gets the user process state.
65 get { return _state; }
74 /// Default cosntructor
76 public WebUserControlView():base()
78 this.Init
+=new EventHandler(WebUserControlView_Init
);
79 this.Load
+=new System
.EventHandler(WebUserControlView_Load
);
85 private void WebUserControlView_Init(object sender
, EventArgs e
)
87 IWindsorContainer container
= ContainerWebAccessorUtil
.ObtainContainer();
90 IStatePersister statePersister
= (IStatePersister
) container
[typeof(IStatePersister
)];
91 _state
= statePersister
.Load();
92 // Acquire current view
93 _state
.CurrentView
= ConfigUtil
.Settings
.GetView(this.Request
.Path
);
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
)
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
131 // The commandName is in the control.ViewState["CommandName"]
132 // wich is protected :-(
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;
147 /// Set the focus on a control
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());
161 /// Set the focus on a control
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());