Page.cs 13 KB

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