Page.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. //
  2. // System.Web.UI.Page
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Specialized;
  13. using System.IO;
  14. using System.Reflection;
  15. using System.Security.Principal;
  16. using System.Text;
  17. using System.Web;
  18. using System.Web.Caching;
  19. using System.Web.SessionState;
  20. namespace System.Web.UI
  21. {
  22. public class Page : TemplateControl, IHttpHandler
  23. {
  24. private string _culture;
  25. private bool _viewState = true;
  26. private bool _viewStateMac = false;
  27. private string _errorPage;
  28. private ArrayList _fileDependencies;
  29. private string _ID;
  30. private bool _isValid;
  31. private HttpServerUtility _server;
  32. private bool _smartNavigation = false;
  33. private TraceContext _trace;
  34. private bool _traceEnabled;
  35. private TraceMode _traceModeValue;
  36. private int _transactionMode;
  37. private string _UICulture;
  38. private HttpContext _context;
  39. private ValidatorCollection _validators;
  40. private bool _visible;
  41. private bool _renderingForm;
  42. private bool _hasForm;
  43. private object _savedViewState;
  44. private ArrayList _requiresPostBack;
  45. #region Fields
  46. protected const string postEventArgumentID = ""; //FIXME
  47. protected const string postEventSourceID = "";
  48. #endregion
  49. #region Constructor
  50. public Page ()
  51. {
  52. Page = this;
  53. }
  54. #endregion
  55. #region Properties
  56. public HttpApplicationState Application
  57. {
  58. get { return _context.Application; }
  59. }
  60. bool AspCompatMode
  61. {
  62. set { throw new NotImplementedException (); }
  63. }
  64. bool Buffer
  65. {
  66. set { Response.BufferOutput = value; }
  67. }
  68. public Cache Cache
  69. {
  70. get { return _context.Cache; }
  71. }
  72. [MonoTODO]
  73. public string ClientTarget
  74. {
  75. get { throw new NotImplementedException (); }
  76. set { throw new NotImplementedException (); }
  77. }
  78. [MonoTODO]
  79. int CodePage
  80. {
  81. set { throw new NotImplementedException (); }
  82. }
  83. [MonoTODO]
  84. string ContentType
  85. {
  86. set { throw new NotImplementedException (); }
  87. }
  88. protected override HttpContext Context
  89. {
  90. get { return _context; }
  91. }
  92. string Culture
  93. {
  94. set { _culture = value; }
  95. }
  96. public override bool EnableViewState
  97. {
  98. get { return _viewState; }
  99. set { _viewState = value; }
  100. }
  101. protected bool EnableViewStateMac
  102. {
  103. get { return _viewStateMac; }
  104. set { _viewStateMac = value; }
  105. }
  106. public string ErrorPage
  107. {
  108. get { return _errorPage; }
  109. set { _errorPage = value; }
  110. }
  111. public ArrayList FileDependencies
  112. {
  113. set { _fileDependencies = value; }
  114. }
  115. public override string ID
  116. {
  117. get { return _ID; }
  118. set { _ID = value; }
  119. }
  120. public bool IsPostBack
  121. {
  122. get {
  123. return (Request.HttpMethod == "POST");
  124. }
  125. }
  126. [MonoTODO]
  127. public bool IsReusable
  128. {
  129. get { throw new NotImplementedException (); }
  130. }
  131. public bool IsValid
  132. {
  133. get { return _isValid; }
  134. }
  135. [MonoTODO]
  136. int LCID {
  137. set { throw new NotImplementedException (); }
  138. }
  139. public HttpRequest Request
  140. {
  141. get { return _context.Request; }
  142. }
  143. public HttpResponse Response
  144. {
  145. get { return _context.Response; }
  146. }
  147. string ResponseEncoding
  148. {
  149. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  150. }
  151. public HttpServerUtility Server
  152. {
  153. get { return _server; }
  154. }
  155. public virtual HttpSessionState Session
  156. {
  157. get { return _context.Session; }
  158. }
  159. public bool SmartNavigation
  160. {
  161. get { return _smartNavigation; }
  162. set { _smartNavigation = value; }
  163. }
  164. public TraceContext Trace
  165. {
  166. get { return _trace; }
  167. }
  168. bool TraceEnabled
  169. {
  170. set { _traceEnabled = value; }
  171. }
  172. TraceMode TraceModeValue
  173. {
  174. set { _traceModeValue = value; }
  175. }
  176. int TransactionMode
  177. {
  178. set { _transactionMode = value; }
  179. }
  180. string UICulture
  181. {
  182. set { _UICulture = value; }
  183. }
  184. public IPrincipal User
  185. {
  186. get { return _context.User; }
  187. }
  188. public ValidatorCollection Validators
  189. {
  190. get {
  191. if (_validators == null)
  192. _validators = new ValidatorCollection ();
  193. return _validators;
  194. }
  195. }
  196. public override bool Visible
  197. {
  198. get { return _visible; }
  199. set { _visible = value; }
  200. }
  201. #endregion
  202. #region Methods
  203. [MonoTODO]
  204. protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
  205. AsyncCallback cb,
  206. object extraData)
  207. {
  208. throw new NotImplementedException ();
  209. }
  210. [MonoTODO]
  211. protected void AspcompatEndProcessRequest (IAsyncResult result)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
  216. {
  217. return new HtmlTextWriter (tw);
  218. }
  219. [MonoTODO]
  220. public void DesignerInitialize ()
  221. {
  222. throw new NotImplementedException ();
  223. }
  224. protected virtual NameValueCollection DeterminePostBackMode ()
  225. {
  226. if (IsPostBack)
  227. return _context.Request.Form;
  228. return _context.Request.QueryString;
  229. }
  230. [MonoTODO]
  231. public string GetPostBackClientEvent (Control control, string argument)
  232. {
  233. // Don't throw the exception. keep going
  234. //throw new NotImplementedException ();
  235. StringBuilder result = new StringBuilder ();
  236. result.AppendFormat ("GetPostBackClientEvent ('{0}', '{1}')", control.ID, argument);
  237. return result.ToString ();
  238. }
  239. [MonoTODO]
  240. public string GetPostBackClientHyperlink (Control control, string argument)
  241. {
  242. throw new NotImplementedException ();
  243. }
  244. [MonoTODO]
  245. public string GetPostBackEventReference (Control control)
  246. {
  247. // Don't throw the exception. keep going
  248. //throw new NotImplementedException ();
  249. return GetPostBackEventReference (control, "");
  250. }
  251. [MonoTODO]
  252. public string GetPostBackEventReference (Control control, string argument)
  253. {
  254. // Don't throw the exception. keep going
  255. //throw new NotImplementedException ();
  256. StringBuilder result = new StringBuilder ();
  257. result.AppendFormat ("GetPostBackEventReference ('{0}', '{1}')", control.ID, argument);
  258. return result.ToString ();
  259. }
  260. public virtual int GetTypeHashCode ()
  261. {
  262. return GetHashCode ();
  263. }
  264. [MonoTODO]
  265. protected virtual void InitOutputCache (int duration,
  266. string varyByHeader,
  267. string varyByCustom,
  268. OutputCacheLocation location,
  269. string varyByParam)
  270. {
  271. throw new NotImplementedException ();
  272. }
  273. [MonoTODO]
  274. public bool IsClientScriptBlockRegistered (string key)
  275. {
  276. throw new NotImplementedException ();
  277. }
  278. [MonoTODO]
  279. public bool IsStartupScriptRegistered (string key)
  280. {
  281. throw new NotImplementedException ();
  282. }
  283. [MonoTODO]
  284. public string MapPath (string virtualPath)
  285. {
  286. throw new NotImplementedException ();
  287. }
  288. private void InvokeEventMethod (string m_name, object sender, EventArgs e)
  289. {
  290. Type [] types = new Type [] {typeof (object), typeof (EventArgs)};
  291. MethodInfo evt_method = GetType ().GetMethod (m_name, types);
  292. if (evt_method != null){
  293. object [] parms = new object [2];
  294. parms [0] = sender;
  295. parms [1] = e;
  296. evt_method.Invoke (this, parms);
  297. }
  298. }
  299. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  300. {
  301. if (_hasForm)
  302. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  303. _renderingForm = true;
  304. _hasForm = true;
  305. writer.WriteLine();
  306. writer.Write("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  307. writer.WriteLine("value=\"{0}\" />", GetTypeHashCode ()); //FIXME: should use a random
  308. // secure value.
  309. }
  310. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  311. {
  312. _renderingForm = false;
  313. }
  314. private void _Page_Init (object sender, EventArgs e)
  315. {
  316. InvokeEventMethod ("Page_Init", sender, e);
  317. }
  318. private void _Page_Load (object sender, EventArgs e)
  319. {
  320. InvokeEventMethod ("Page_Load", sender, e);
  321. }
  322. private void ProcessPostData ()
  323. {
  324. NameValueCollection data = DeterminePostBackMode ();
  325. ArrayList _raisePostBack = new ArrayList ();
  326. foreach (string id in data.AllKeys){
  327. string value = data [id];
  328. Control ctrl = FindControl (id);
  329. if (ctrl != null){
  330. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  331. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  332. if (pbdh != null) {
  333. if (pbdh.LoadPostData (id, data) == true) {
  334. pbdh.RaisePostDataChangedEvent ();
  335. if (pbeh == null)
  336. continue;
  337. if (_requiresPostBack != null &&
  338. !(_requiresPostBack.Contains (ctrl)))
  339. _raisePostBack.Add (pbeh);
  340. }
  341. continue;
  342. }
  343. if (pbeh != null)
  344. pbeh.RaisePostBackEvent (null);
  345. }
  346. }
  347. foreach (IPostBackEventHandler e in _raisePostBack)
  348. e.RaisePostBackEvent (null);
  349. if (_requiresPostBack != null)
  350. foreach (IPostBackEventHandler e in _requiresPostBack)
  351. e.RaisePostBackEvent (null);
  352. }
  353. private bool init_done;
  354. public void ProcessRequest (HttpContext context)
  355. {
  356. if (!init_done){
  357. init_done = true;
  358. FrameworkInitialize ();
  359. // This 2 should depend on AutoEventWireUp in Page directive. Defaults to true.
  360. Init += new EventHandler (_Page_Init);
  361. Load += new EventHandler (_Page_Load);
  362. //-- Control execution lifecycle in the docs
  363. OnInit (EventArgs.Empty);
  364. }
  365. _hasForm = false;
  366. _context = context;
  367. //LoadViewState ();
  368. //if (this is IPostBackDataHandler)
  369. // LoadPostData ();
  370. if (IsPostBack)
  371. ProcessPostData ();
  372. OnLoad (EventArgs.Empty);
  373. //if (this is IPostBackDataHandler)
  374. // RaisePostBackEvent ();
  375. OnPreRender (EventArgs.Empty);
  376. //--
  377. HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
  378. foreach (Control ctrl in Controls)
  379. ctrl.RenderControl (output);
  380. }
  381. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  382. {
  383. sourceControl.RaisePostBackEvent (eventArgument);
  384. }
  385. [MonoTODO]
  386. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  387. {
  388. throw new NotImplementedException ();
  389. }
  390. [MonoTODO]
  391. public virtual void RegisterClientScriptBlock (string key, string script)
  392. {
  393. throw new NotImplementedException ();
  394. }
  395. [MonoTODO]
  396. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  397. {
  398. throw new NotImplementedException ();
  399. }
  400. [MonoTODO]
  401. public void RegisterClientScriptFile (string a, string b, string c)
  402. {
  403. throw new NotImplementedException ();
  404. }
  405. [MonoTODO]
  406. public void RegisterOnSubmitStatement (string key, string script)
  407. {
  408. throw new NotImplementedException ();
  409. }
  410. public void RegisterRequiresPostBack (Control control)
  411. {
  412. if (_requiresPostBack == null)
  413. _requiresPostBack = new ArrayList ();
  414. _requiresPostBack.Add (control);
  415. }
  416. [MonoTODO]
  417. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  418. {
  419. throw new NotImplementedException ();
  420. }
  421. [MonoTODO]
  422. public void RegisterViewStateHandler ()
  423. {
  424. throw new NotImplementedException ();
  425. }
  426. [MonoTODO]
  427. protected virtual void SavePageStatetoPersistenceMedium (object viewState)
  428. {
  429. throw new NotImplementedException ();
  430. }
  431. protected virtual object LoadPageStateFromPersistenceMedium ()
  432. {
  433. return _savedViewState;
  434. }
  435. internal void LoadPageViewState()
  436. {
  437. object sState = LoadPageStateFromPersistenceMedium ();
  438. if (sState != null)
  439. LoadViewStateRecursive (sState);
  440. }
  441. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  442. {
  443. _savedViewState = viewState;
  444. }
  445. internal void SavePageViewState ()
  446. {
  447. SavePageStateToPersistenceMedium (SaveViewStateRecursive ());
  448. }
  449. public virtual void Validate ()
  450. {
  451. if (_validators == null && _validators.Count == 0){
  452. _isValid = true;
  453. return;
  454. }
  455. bool all_valid = true;
  456. foreach (IValidator v in _validators){
  457. v.Validate ();
  458. if (v.IsValid == false)
  459. all_valid = false;
  460. }
  461. if (all_valid)
  462. _isValid = true;
  463. }
  464. public virtual void VerifyRenderingInServerForm (Control control)
  465. {
  466. if (!_renderingForm)
  467. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  468. "' must be rendered within a HtmlForm");
  469. }
  470. #endregion
  471. }
  472. }