Page.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. //
  2. // System.Web.UI.Page.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  10. // (c) 2003 Novell, Inc. (http://www.novell.com)
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Specialized;
  35. using System.ComponentModel;
  36. using System.ComponentModel.Design;
  37. using System.ComponentModel.Design.Serialization;
  38. using System.Globalization;
  39. using System.IO;
  40. using System.Security.Principal;
  41. using System.Text;
  42. using System.Threading;
  43. using System.Web;
  44. using System.Web.Caching;
  45. using System.Web.SessionState;
  46. using System.Web.Util;
  47. namespace System.Web.UI
  48. {
  49. [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
  50. [ToolboxItem (false)]
  51. [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  52. [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
  53. public class Page : TemplateControl, IHttpHandler
  54. {
  55. private bool _viewState = true;
  56. private bool _viewStateMac;
  57. private string _errorPage;
  58. private bool _isValid;
  59. private bool _smartNavigation;
  60. private int _transactionMode;
  61. private HttpContext _context;
  62. private ValidatorCollection _validators;
  63. private bool renderingForm;
  64. private object _savedViewState;
  65. private ArrayList _requiresPostBack;
  66. private ArrayList _requiresPostBackCopy;
  67. private ArrayList requiresPostDataChanged;
  68. private IPostBackEventHandler requiresRaiseEvent;
  69. private NameValueCollection secondPostData;
  70. private bool requiresPostBackScript;
  71. private bool postBackScriptRendered;
  72. private Hashtable registeredArrayDeclares;
  73. Hashtable clientScriptBlocks;
  74. Hashtable startupScriptBlocks;
  75. Hashtable hiddenFields;
  76. internal Hashtable submitStatements;
  77. bool handleViewState;
  78. string viewStateUserKey;
  79. NameValueCollection _requestValueCollection;
  80. string clientTarget;
  81. [EditorBrowsable (EditorBrowsableState.Never)]
  82. protected const string postEventArgumentID = "__EVENTARGUMENT";
  83. [EditorBrowsable (EditorBrowsableState.Never)]
  84. protected const string postEventSourceID = "__EVENTTARGET";
  85. #region Constructor
  86. public Page ()
  87. {
  88. Page = this;
  89. }
  90. #endregion
  91. #region Properties
  92. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  93. [Browsable (false)]
  94. public HttpApplicationState Application
  95. {
  96. get { return _context.Application; }
  97. }
  98. [EditorBrowsable (EditorBrowsableState.Never)]
  99. protected bool AspCompatMode
  100. {
  101. set { throw new NotImplementedException (); }
  102. }
  103. [EditorBrowsable (EditorBrowsableState.Never)]
  104. protected bool Buffer
  105. {
  106. set { Response.BufferOutput = value; }
  107. }
  108. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  109. [Browsable (false)]
  110. public Cache Cache
  111. {
  112. get { return _context.Cache; }
  113. }
  114. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  115. [Browsable (false), DefaultValue ("")]
  116. [WebSysDescription ("Value do override the automatic browser detection and force the page to use the specified browser.")]
  117. public string ClientTarget
  118. {
  119. get { return (clientTarget == null) ? "" : clientTarget; }
  120. set {
  121. clientTarget = value;
  122. if (value == "")
  123. clientTarget = null;
  124. }
  125. }
  126. [EditorBrowsable (EditorBrowsableState.Never)]
  127. protected int CodePage
  128. {
  129. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  130. }
  131. [EditorBrowsable (EditorBrowsableState.Never)]
  132. protected string ContentType
  133. {
  134. set { Response.ContentType = value; }
  135. }
  136. protected override HttpContext Context
  137. {
  138. get {
  139. if (_context == null)
  140. return HttpContext.Current;
  141. return _context;
  142. }
  143. }
  144. [EditorBrowsable (EditorBrowsableState.Never)]
  145. protected string Culture
  146. {
  147. set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
  148. }
  149. [Browsable (false)]
  150. public override bool EnableViewState
  151. {
  152. get { return _viewState; }
  153. set { _viewState = value; }
  154. }
  155. [EditorBrowsable (EditorBrowsableState.Never)]
  156. protected bool EnableViewStateMac
  157. {
  158. get { return _viewStateMac; }
  159. set { _viewStateMac = value; }
  160. }
  161. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  162. [Browsable (false), DefaultValue ("")]
  163. [WebSysDescription ("The URL of a page used for error redirection.")]
  164. public string ErrorPage
  165. {
  166. get { return _errorPage; }
  167. set {
  168. _errorPage = value;
  169. if (_context != null)
  170. _context.ErrorPage = value;
  171. }
  172. }
  173. [EditorBrowsable (EditorBrowsableState.Never)]
  174. protected ArrayList FileDependencies
  175. {
  176. set {
  177. if (Response != null)
  178. Response.AddFileDependencies (value);
  179. }
  180. }
  181. [Browsable (false)]
  182. public override string ID
  183. {
  184. get { return base.ID; }
  185. set { base.ID = value; }
  186. }
  187. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  188. [Browsable (false)]
  189. public bool IsPostBack
  190. {
  191. get {
  192. return _requestValueCollection != null;
  193. }
  194. }
  195. [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
  196. public bool IsReusable {
  197. get { return false; }
  198. }
  199. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  200. [Browsable (false)]
  201. public bool IsValid
  202. {
  203. get { return _isValid; }
  204. }
  205. [EditorBrowsable (EditorBrowsableState.Never)]
  206. protected int LCID {
  207. set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
  208. }
  209. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  210. [Browsable (false)]
  211. public HttpRequest Request
  212. {
  213. get { return _context.Request; }
  214. }
  215. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  216. [Browsable (false)]
  217. public HttpResponse Response
  218. {
  219. get { return _context.Response; }
  220. }
  221. [EditorBrowsable (EditorBrowsableState.Never)]
  222. protected string ResponseEncoding
  223. {
  224. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  225. }
  226. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  227. [Browsable (false)]
  228. public HttpServerUtility Server
  229. {
  230. get {
  231. return Context.Server;
  232. }
  233. }
  234. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  235. [Browsable (false)]
  236. public virtual HttpSessionState Session
  237. {
  238. get {
  239. if (_context.Session == null)
  240. throw new HttpException ("Session state can only be used " +
  241. "when enableSessionState is set to true, either " +
  242. "in a configuration file or in the Page directive.");
  243. return _context.Session;
  244. }
  245. }
  246. [Browsable (false)]
  247. public bool SmartNavigation
  248. {
  249. get { return _smartNavigation; }
  250. set { _smartNavigation = value; }
  251. }
  252. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  253. [Browsable (false)]
  254. public TraceContext Trace
  255. {
  256. get { return Context.Trace; }
  257. }
  258. [EditorBrowsable (EditorBrowsableState.Never)]
  259. protected bool TraceEnabled
  260. {
  261. set { Trace.IsEnabled = value; }
  262. }
  263. [EditorBrowsable (EditorBrowsableState.Never)]
  264. protected TraceMode TraceModeValue
  265. {
  266. set { Trace.TraceMode = value; }
  267. }
  268. [EditorBrowsable (EditorBrowsableState.Never)]
  269. protected int TransactionMode
  270. {
  271. set { _transactionMode = value; }
  272. }
  273. [EditorBrowsable (EditorBrowsableState.Never)]
  274. protected string UICulture
  275. {
  276. set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
  277. }
  278. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  279. [Browsable (false)]
  280. public IPrincipal User
  281. {
  282. get { return _context.User; }
  283. }
  284. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  285. [Browsable (false)]
  286. public ValidatorCollection Validators
  287. {
  288. get {
  289. if (_validators == null)
  290. _validators = new ValidatorCollection ();
  291. return _validators;
  292. }
  293. }
  294. [MonoTODO ("Use this when encrypting/decrypting ViewState")]
  295. [Browsable (false)]
  296. public string ViewStateUserKey {
  297. get { return viewStateUserKey; }
  298. set { viewStateUserKey = value; }
  299. }
  300. [Browsable (false)]
  301. public override bool Visible
  302. {
  303. get { return base.Visible; }
  304. set { base.Visible = value; }
  305. }
  306. #endregion
  307. #region Methods
  308. [EditorBrowsable (EditorBrowsableState.Never)]
  309. protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
  310. AsyncCallback cb,
  311. object extraData)
  312. {
  313. throw new NotImplementedException ();
  314. }
  315. [EditorBrowsable (EditorBrowsableState.Never)]
  316. protected void AspCompatEndProcessRequest (IAsyncResult result)
  317. {
  318. throw new NotImplementedException ();
  319. }
  320. [EditorBrowsable (EditorBrowsableState.Advanced)]
  321. protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
  322. {
  323. return new HtmlTextWriter (tw);
  324. }
  325. [EditorBrowsable (EditorBrowsableState.Never)]
  326. public void DesignerInitialize ()
  327. {
  328. InitRecursive (null);
  329. }
  330. [EditorBrowsable (EditorBrowsableState.Advanced)]
  331. protected virtual NameValueCollection DeterminePostBackMode ()
  332. {
  333. if (_context == null)
  334. return null;
  335. HttpRequest req = _context.Request;
  336. if (req == null)
  337. return null;
  338. NameValueCollection coll = null;
  339. if (0 == String.Compare (Request.HttpMethod, "POST", true))
  340. coll = req.Form;
  341. else
  342. coll = req.QueryString;
  343. if (coll == null || coll ["__VIEWSTATE"] == null)
  344. return null;
  345. return coll;
  346. }
  347. [EditorBrowsable (EditorBrowsableState.Advanced)]
  348. public string GetPostBackClientEvent (Control control, string argument)
  349. {
  350. return GetPostBackEventReference (control, argument);
  351. }
  352. [EditorBrowsable (EditorBrowsableState.Advanced)]
  353. public string GetPostBackClientHyperlink (Control control, string argument)
  354. {
  355. return "javascript:" + GetPostBackEventReference (control, argument);
  356. }
  357. [EditorBrowsable (EditorBrowsableState.Advanced)]
  358. public string GetPostBackEventReference (Control control)
  359. {
  360. return GetPostBackEventReference (control, "");
  361. }
  362. [EditorBrowsable (EditorBrowsableState.Advanced)]
  363. public string GetPostBackEventReference (Control control, string argument)
  364. {
  365. RequiresPostBackScript ();
  366. return String.Format ("__doPostBack('{0}','{1}')", control.UniqueID, argument);
  367. }
  368. internal void RequiresPostBackScript ()
  369. {
  370. requiresPostBackScript = true;
  371. }
  372. [EditorBrowsable (EditorBrowsableState.Never)]
  373. public virtual int GetTypeHashCode ()
  374. {
  375. return 0;
  376. }
  377. [EditorBrowsable (EditorBrowsableState.Never)]
  378. protected virtual void InitOutputCache (int duration,
  379. string varyByHeader,
  380. string varyByCustom,
  381. OutputCacheLocation location,
  382. string varyByParam)
  383. {
  384. HttpCachePolicy cache = _context.Response.Cache;
  385. bool set_vary = false;
  386. switch (location) {
  387. case OutputCacheLocation.Any:
  388. cache.SetCacheability (HttpCacheability.Public);
  389. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  390. cache.SetLastModified (_context.Timestamp);
  391. set_vary = true;
  392. break;
  393. case OutputCacheLocation.Client:
  394. cache.SetCacheability (HttpCacheability.Private);
  395. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  396. cache.SetLastModified (_context.Timestamp);
  397. break;
  398. case OutputCacheLocation.Downstream:
  399. cache.SetCacheability (HttpCacheability.Public);
  400. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  401. cache.SetLastModified (_context.Timestamp);
  402. break;
  403. case OutputCacheLocation.Server:
  404. cache.SetCacheability (HttpCacheability.Server);
  405. set_vary = true;
  406. break;
  407. case OutputCacheLocation.None:
  408. break;
  409. }
  410. if (set_vary) {
  411. if (varyByCustom != null)
  412. cache.SetVaryByCustom (varyByCustom);
  413. if (varyByParam != null && varyByParam.Length > 0) {
  414. string[] prms = varyByParam.Split (';');
  415. foreach (string p in prms)
  416. cache.VaryByParams [p.Trim ()] = true;
  417. cache.VaryByParams.IgnoreParams = false;
  418. } else {
  419. cache.VaryByParams.IgnoreParams = true;
  420. }
  421. if (varyByHeader != null && varyByHeader.Length > 0) {
  422. string[] hdrs = varyByHeader.Split (';');
  423. foreach (string h in hdrs)
  424. cache.VaryByHeaders [h.Trim ()] = true;
  425. }
  426. }
  427. cache.Duration = duration;
  428. cache.SetExpires (_context.Timestamp.AddSeconds (duration));
  429. }
  430. [EditorBrowsable (EditorBrowsableState.Advanced)]
  431. public bool IsClientScriptBlockRegistered (string key)
  432. {
  433. if (clientScriptBlocks == null)
  434. return false;
  435. return clientScriptBlocks.ContainsKey (key);
  436. }
  437. [EditorBrowsable (EditorBrowsableState.Advanced)]
  438. public bool IsStartupScriptRegistered (string key)
  439. {
  440. if (startupScriptBlocks == null)
  441. return false;
  442. return startupScriptBlocks.ContainsKey (key);
  443. }
  444. public string MapPath (string virtualPath)
  445. {
  446. return Request.MapPath (virtualPath);
  447. }
  448. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  449. {
  450. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
  451. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
  452. writer.WriteLine ();
  453. writer.WriteLine ("<script language=\"javascript\">");
  454. writer.WriteLine ("<!--");
  455. writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
  456. writer.WriteLine ("\t\tvar theform = document.getElementById ('{0}');", formUniqueID);
  457. writer.WriteLine ("\t\ttheform.{0}.value = eventTarget;", postEventSourceID);
  458. writer.WriteLine ("\t\ttheform.{0}.value = eventArgument;", postEventArgumentID);
  459. writer.WriteLine ("\t\ttheform.submit();");
  460. writer.WriteLine ("\t}");
  461. writer.WriteLine ("// -->");
  462. writer.WriteLine ("</script>");
  463. }
  464. static void WriteScripts (HtmlTextWriter writer, Hashtable scripts)
  465. {
  466. if (scripts == null)
  467. return;
  468. foreach (string key in scripts.Values)
  469. writer.WriteLine (key);
  470. }
  471. void WriteHiddenFields (HtmlTextWriter writer)
  472. {
  473. if (hiddenFields == null)
  474. return;
  475. foreach (string key in hiddenFields.Keys) {
  476. string value = hiddenFields [key] as string;
  477. writer.WriteLine ("\n<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", key, value);
  478. }
  479. hiddenFields = null;
  480. }
  481. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  482. {
  483. if (renderingForm)
  484. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  485. renderingForm = true;
  486. writer.WriteLine ();
  487. WriteHiddenFields (writer);
  488. if (requiresPostBackScript) {
  489. RenderPostBackScript (writer, formUniqueID);
  490. postBackScriptRendered = true;
  491. }
  492. if (handleViewState) {
  493. string vs = GetViewStateString ();
  494. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  495. writer.WriteLine ("value=\"{0}\" />", vs);
  496. }
  497. WriteScripts (writer, clientScriptBlocks);
  498. }
  499. internal string GetViewStateString ()
  500. {
  501. StringWriter sr = new StringWriter ();
  502. LosFormatter fmt = new LosFormatter ();
  503. fmt.Serialize (sr, _savedViewState);
  504. return sr.GetStringBuilder ().ToString ();
  505. }
  506. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  507. {
  508. if (registeredArrayDeclares != null) {
  509. writer.WriteLine();
  510. writer.WriteLine("<script language=\"javascript\">");
  511. writer.WriteLine("<!--");
  512. IDictionaryEnumerator arrayEnum = registeredArrayDeclares.GetEnumerator();
  513. while (arrayEnum.MoveNext()) {
  514. writer.Write("\tvar ");
  515. writer.Write(arrayEnum.Key);
  516. writer.Write(" = new Array(");
  517. IEnumerator arrayListEnum = ((ArrayList) arrayEnum.Value).GetEnumerator();
  518. bool isFirst = true;
  519. while (arrayListEnum.MoveNext()) {
  520. if (isFirst)
  521. isFirst = false;
  522. else
  523. writer.Write(", ");
  524. writer.Write(arrayListEnum.Current);
  525. }
  526. writer.WriteLine(");");
  527. }
  528. writer.WriteLine("// -->");
  529. writer.WriteLine("</script>");
  530. writer.WriteLine();
  531. }
  532. if (!postBackScriptRendered && requiresPostBackScript)
  533. RenderPostBackScript (writer, formUniqueID);
  534. WriteHiddenFields (writer);
  535. WriteScripts (writer, startupScriptBlocks);
  536. renderingForm = false;
  537. postBackScriptRendered = false;
  538. }
  539. private void ProcessPostData (NameValueCollection data, bool second)
  540. {
  541. if (data == null)
  542. return;
  543. if (_requiresPostBackCopy == null && _requiresPostBack != null)
  544. _requiresPostBackCopy = (ArrayList) _requiresPostBack.Clone ();
  545. Hashtable used = new Hashtable ();
  546. foreach (string id in data.AllKeys){
  547. if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
  548. continue;
  549. string real_id = id;
  550. int dot = real_id.IndexOf ('.');
  551. if (dot >= 1)
  552. real_id = real_id.Substring (0, dot);
  553. if (real_id == null || used.ContainsKey (real_id))
  554. continue;
  555. used.Add (real_id, real_id);
  556. Control ctrl = FindControl (real_id);
  557. if (ctrl != null){
  558. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  559. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  560. if (pbdh == null) {
  561. if (pbeh != null)
  562. RegisterRequiresRaiseEvent (pbeh);
  563. continue;
  564. }
  565. if (pbdh.LoadPostData (real_id, data) == true) {
  566. if (requiresPostDataChanged == null)
  567. requiresPostDataChanged = new ArrayList ();
  568. requiresPostDataChanged.Add (pbdh);
  569. if (_requiresPostBackCopy != null)
  570. _requiresPostBackCopy.Remove (ctrl.UniqueID);
  571. }
  572. } else if (!second) {
  573. if (secondPostData == null)
  574. secondPostData = new NameValueCollection ();
  575. secondPostData.Add (real_id, data [id]);
  576. }
  577. }
  578. ArrayList list1 = null;
  579. if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
  580. string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
  581. foreach (string id in handlers) {
  582. IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
  583. if (pbdh != null) {
  584. _requiresPostBackCopy.Remove (id);
  585. if (pbdh.LoadPostData (id, data)) {
  586. if (requiresPostDataChanged == null)
  587. requiresPostDataChanged = new ArrayList ();
  588. requiresPostDataChanged.Add (pbdh);
  589. }
  590. } else if (second) {
  591. if (list1 == null)
  592. list1 = new ArrayList ();
  593. list1.Add (id);
  594. }
  595. }
  596. _requiresPostBack = list1;
  597. }
  598. }
  599. [EditorBrowsable (EditorBrowsableState.Never)]
  600. public void ProcessRequest (HttpContext context)
  601. {
  602. _context = context;
  603. if (clientTarget != null)
  604. Request.ClientTarget = clientTarget;
  605. WireupAutomaticEvents ();
  606. //-- Control execution lifecycle in the docs
  607. // Save culture information because it can be modified in FrameworkInitialize()
  608. CultureInfo culture = Thread.CurrentThread.CurrentCulture;
  609. CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
  610. FrameworkInitialize ();
  611. context.ErrorPage = _errorPage;
  612. try {
  613. InternalProcessRequest ();
  614. } finally {
  615. try {
  616. UnloadRecursive (true);
  617. } catch {}
  618. Thread.CurrentThread.CurrentCulture = culture;
  619. Thread.CurrentThread.CurrentUICulture = uiculture;
  620. }
  621. }
  622. void InternalProcessRequest ()
  623. {
  624. _requestValueCollection = this.DeterminePostBackMode();
  625. Trace.Write ("aspx.page", "Begin Init");
  626. InitRecursive (null);
  627. Trace.Write ("aspx.page", "End Init");
  628. renderingForm = false;
  629. if (IsPostBack) {
  630. Trace.Write ("aspx.page", "Begin LoadViewState");
  631. LoadPageViewState ();
  632. Trace.Write ("aspx.page", "End LoadViewState");
  633. Trace.Write ("aspx.page", "Begin ProcessPostData");
  634. ProcessPostData (_requestValueCollection, false);
  635. Trace.Write ("aspx.page", "End ProcessPostData");
  636. }
  637. LoadRecursive ();
  638. if (IsPostBack) {
  639. Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
  640. ProcessPostData (secondPostData, true);
  641. Trace.Write ("aspx.page", "End ProcessPostData Second Try");
  642. Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
  643. RaiseChangedEvents ();
  644. Trace.Write ("aspx.page", "End Raise ChangedEvents");
  645. Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
  646. RaisePostBackEvents ();
  647. Trace.Write ("aspx.page", "End Raise PostBackEvent");
  648. }
  649. Trace.Write ("aspx.page", "Begin PreRender");
  650. PreRenderRecursiveInternal ();
  651. Trace.Write ("aspx.page", "End PreRender");
  652. Trace.Write ("aspx.page", "Begin SaveViewState");
  653. SavePageViewState ();
  654. Trace.Write ("aspx.page", "End SaveViewState");
  655. //--
  656. Trace.Write ("aspx.page", "Begin Render");
  657. HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
  658. RenderControl (output);
  659. Trace.Write ("aspx.page", "End Render");
  660. RenderTrace (output);
  661. }
  662. private void RenderTrace (HtmlTextWriter output)
  663. {
  664. TraceManager traceManager = HttpRuntime.TraceManager;
  665. if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
  666. return;
  667. Trace.SaveData ();
  668. if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput)
  669. return;
  670. if (!traceManager.LocalOnly || Context.Request.IsLocal)
  671. Trace.Render (output);
  672. }
  673. internal void RaisePostBackEvents ()
  674. {
  675. if (requiresRaiseEvent != null) {
  676. RaisePostBackEvent (requiresRaiseEvent, null);
  677. return;
  678. }
  679. NameValueCollection postdata = _requestValueCollection;
  680. if (postdata == null)
  681. return;
  682. string eventTarget = postdata [postEventSourceID];
  683. if (eventTarget == null || eventTarget.Length == 0) {
  684. Validate ();
  685. return;
  686. }
  687. IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
  688. if (target == null)
  689. return;
  690. string eventArgument = postdata [postEventArgumentID];
  691. RaisePostBackEvent (target, eventArgument);
  692. }
  693. internal void RaiseChangedEvents ()
  694. {
  695. if (requiresPostDataChanged == null)
  696. return;
  697. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  698. ipdh.RaisePostDataChangedEvent ();
  699. requiresPostDataChanged = null;
  700. }
  701. [EditorBrowsable (EditorBrowsableState.Advanced)]
  702. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  703. {
  704. sourceControl.RaisePostBackEvent (eventArgument);
  705. }
  706. [EditorBrowsable (EditorBrowsableState.Advanced)]
  707. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  708. {
  709. if (registeredArrayDeclares == null)
  710. registeredArrayDeclares = new Hashtable();
  711. if (!registeredArrayDeclares.ContainsKey (arrayName))
  712. registeredArrayDeclares.Add (arrayName, new ArrayList());
  713. ((ArrayList) registeredArrayDeclares[arrayName]).Add(arrayValue);
  714. }
  715. [EditorBrowsable (EditorBrowsableState.Advanced)]
  716. public virtual void RegisterClientScriptBlock (string key, string script)
  717. {
  718. if (IsClientScriptBlockRegistered (key))
  719. return;
  720. if (clientScriptBlocks == null)
  721. clientScriptBlocks = new Hashtable ();
  722. clientScriptBlocks.Add (key, script);
  723. }
  724. [EditorBrowsable (EditorBrowsableState.Advanced)]
  725. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  726. {
  727. if (hiddenFields == null)
  728. hiddenFields = new Hashtable ();
  729. if (!hiddenFields.ContainsKey (hiddenFieldName))
  730. hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
  731. }
  732. [MonoTODO("Used in HtmlForm")]
  733. internal void RegisterClientScriptFile (string a, string b, string c)
  734. {
  735. throw new NotImplementedException ();
  736. }
  737. [EditorBrowsable (EditorBrowsableState.Advanced)]
  738. public void RegisterOnSubmitStatement (string key, string script)
  739. {
  740. if (submitStatements == null)
  741. submitStatements = new Hashtable ();
  742. if (submitStatements.ContainsKey (key))
  743. return;
  744. submitStatements.Add (key, script);
  745. }
  746. [EditorBrowsable (EditorBrowsableState.Advanced)]
  747. public void RegisterRequiresPostBack (Control control)
  748. {
  749. if (_requiresPostBack == null)
  750. _requiresPostBack = new ArrayList ();
  751. _requiresPostBack.Add (control.UniqueID);
  752. }
  753. [EditorBrowsable (EditorBrowsableState.Advanced)]
  754. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  755. {
  756. requiresRaiseEvent = control;
  757. }
  758. [EditorBrowsable (EditorBrowsableState.Advanced)]
  759. public virtual void RegisterStartupScript (string key, string script)
  760. {
  761. if (IsStartupScriptRegistered (key))
  762. return;
  763. if (startupScriptBlocks == null)
  764. startupScriptBlocks = new Hashtable ();
  765. startupScriptBlocks.Add (key, script);
  766. }
  767. [EditorBrowsable (EditorBrowsableState.Advanced)]
  768. public void RegisterViewStateHandler ()
  769. {
  770. handleViewState = true;
  771. }
  772. [EditorBrowsable (EditorBrowsableState.Advanced)]
  773. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  774. {
  775. _savedViewState = viewState;
  776. }
  777. [EditorBrowsable (EditorBrowsableState.Advanced)]
  778. protected virtual object LoadPageStateFromPersistenceMedium ()
  779. {
  780. NameValueCollection postdata = _requestValueCollection;
  781. string view_state;
  782. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  783. return null;
  784. _savedViewState = null;
  785. LosFormatter fmt = new LosFormatter ();
  786. try {
  787. _savedViewState = fmt.Deserialize (view_state);
  788. } catch (Exception e) {
  789. throw new HttpException ("Error restoring page viewstate.\n", e);
  790. }
  791. return _savedViewState;
  792. }
  793. internal void LoadPageViewState()
  794. {
  795. object sState = LoadPageStateFromPersistenceMedium ();
  796. if (sState != null) {
  797. Pair pair = (Pair) sState;
  798. LoadViewStateRecursive (pair.First);
  799. _requiresPostBack = pair.Second as ArrayList;
  800. }
  801. }
  802. internal void SavePageViewState ()
  803. {
  804. if (!handleViewState)
  805. return;
  806. Pair pair = new Pair ();
  807. pair.First = SaveViewStateRecursive ();
  808. if (_requiresPostBack != null && _requiresPostBack.Count > 0)
  809. pair.Second = _requiresPostBack;
  810. if (pair.First == null && pair.Second == null)
  811. pair = null;
  812. SavePageStateToPersistenceMedium (pair);
  813. }
  814. public virtual void Validate ()
  815. {
  816. if (_validators == null || _validators.Count == 0){
  817. _isValid = true;
  818. return;
  819. }
  820. bool all_valid = true;
  821. foreach (IValidator v in _validators){
  822. v.Validate ();
  823. if (v.IsValid == false)
  824. all_valid = false;
  825. }
  826. if (all_valid)
  827. _isValid = true;
  828. }
  829. [EditorBrowsable (EditorBrowsableState.Advanced)]
  830. public virtual void VerifyRenderingInServerForm (Control control)
  831. {
  832. if (!renderingForm)
  833. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  834. "' must be rendered within a HtmlForm");
  835. }
  836. #endregion
  837. #if NET_2_0
  838. public string GetWebResourceUrl(Type type, string resourceName)
  839. {
  840. if (type == null)
  841. throw new ArgumentNullException ("type");
  842. if (resourceName == null || resourceName.Length == 0)
  843. throw new ArgumentNullException ("type");
  844. return System.Web.Handlers.AssemblyResourceLoader.GetResourceUrl (type, resourceName);
  845. }
  846. Stack dataItemCtx;
  847. internal void PushDataItemContext (object o)
  848. {
  849. if (dataItemCtx == null)
  850. dataItemCtx = new Stack ();
  851. dataItemCtx.Push (o);
  852. }
  853. internal void PopDataItemContext ()
  854. {
  855. if (dataItemCtx == null)
  856. throw new InvalidOperationException ();
  857. dataItemCtx.Pop ();
  858. }
  859. internal object CurrentDataItem {
  860. get {
  861. if (dataItemCtx == null)
  862. throw new InvalidOperationException ("No data item");
  863. return dataItemCtx.Peek ();
  864. }
  865. }
  866. protected object Eval (string expression)
  867. {
  868. return DataBinder.Eval (CurrentDataItem, expression);
  869. }
  870. protected object Eval (string expression, string format)
  871. {
  872. return DataBinder.Eval (CurrentDataItem, expression, format);
  873. }
  874. protected object XPath (string xpathexpression)
  875. {
  876. return XPathBinder.Eval (CurrentDataItem, xpathexpression);
  877. }
  878. protected object XPath (string xpathexpression, string format)
  879. {
  880. return XPathBinder.Eval (CurrentDataItem, xpathexpression, format);
  881. }
  882. protected IEnumerable XPathSelect (string xpathexpression)
  883. {
  884. return XPathBinder.Select (CurrentDataItem, xpathexpression);
  885. }
  886. #endif
  887. }
  888. }