Page.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. if (Request.Browser.Browser == ("Netscape") && Request.Browser.MajorVersion == 4)
  457. writer.WriteLine ("\t\tvar theform = document.{0};", formUniqueID);
  458. else
  459. writer.WriteLine ("\t\tvar theform = document.getElementById ('{0}');", formUniqueID);
  460. writer.WriteLine ("\t\ttheform.{0}.value = eventTarget;", postEventSourceID);
  461. writer.WriteLine ("\t\ttheform.{0}.value = eventArgument;", postEventArgumentID);
  462. writer.WriteLine ("\t\ttheform.submit();");
  463. writer.WriteLine ("\t}");
  464. writer.WriteLine ("// -->");
  465. writer.WriteLine ("</script>");
  466. }
  467. static void WriteScripts (HtmlTextWriter writer, Hashtable scripts)
  468. {
  469. if (scripts == null)
  470. return;
  471. foreach (string key in scripts.Values)
  472. writer.WriteLine (key);
  473. }
  474. void WriteHiddenFields (HtmlTextWriter writer)
  475. {
  476. if (hiddenFields == null)
  477. return;
  478. foreach (string key in hiddenFields.Keys) {
  479. string value = hiddenFields [key] as string;
  480. writer.WriteLine ("\n<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", key, value);
  481. }
  482. hiddenFields = null;
  483. }
  484. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  485. {
  486. if (renderingForm)
  487. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  488. renderingForm = true;
  489. writer.WriteLine ();
  490. WriteHiddenFields (writer);
  491. if (requiresPostBackScript) {
  492. RenderPostBackScript (writer, formUniqueID);
  493. postBackScriptRendered = true;
  494. }
  495. if (handleViewState) {
  496. string vs = GetViewStateString ();
  497. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  498. writer.WriteLine ("value=\"{0}\" />", vs);
  499. }
  500. WriteScripts (writer, clientScriptBlocks);
  501. }
  502. internal string GetViewStateString ()
  503. {
  504. if (_savedViewState == null)
  505. return null;
  506. StringWriter sr = new StringWriter ();
  507. LosFormatter fmt = new LosFormatter ();
  508. fmt.Serialize (sr, _savedViewState);
  509. return sr.GetStringBuilder ().ToString ();
  510. }
  511. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  512. {
  513. if (registeredArrayDeclares != null) {
  514. writer.WriteLine();
  515. writer.WriteLine("<script language=\"javascript\">");
  516. writer.WriteLine("<!--");
  517. IDictionaryEnumerator arrayEnum = registeredArrayDeclares.GetEnumerator();
  518. while (arrayEnum.MoveNext()) {
  519. writer.Write("\tvar ");
  520. writer.Write(arrayEnum.Key);
  521. writer.Write(" = new Array(");
  522. IEnumerator arrayListEnum = ((ArrayList) arrayEnum.Value).GetEnumerator();
  523. bool isFirst = true;
  524. while (arrayListEnum.MoveNext()) {
  525. if (isFirst)
  526. isFirst = false;
  527. else
  528. writer.Write(", ");
  529. writer.Write(arrayListEnum.Current);
  530. }
  531. writer.WriteLine(");");
  532. }
  533. writer.WriteLine("// -->");
  534. writer.WriteLine("</script>");
  535. writer.WriteLine();
  536. }
  537. if (!postBackScriptRendered && requiresPostBackScript)
  538. RenderPostBackScript (writer, formUniqueID);
  539. WriteHiddenFields (writer);
  540. WriteScripts (writer, startupScriptBlocks);
  541. renderingForm = false;
  542. postBackScriptRendered = false;
  543. }
  544. private void ProcessPostData (NameValueCollection data, bool second)
  545. {
  546. if (data == null)
  547. return;
  548. if (_requiresPostBackCopy == null && _requiresPostBack != null)
  549. _requiresPostBackCopy = (ArrayList) _requiresPostBack.Clone ();
  550. Hashtable used = new Hashtable ();
  551. foreach (string id in data.AllKeys){
  552. if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
  553. continue;
  554. string real_id = id;
  555. int dot = real_id.IndexOf ('.');
  556. if (dot >= 1)
  557. real_id = real_id.Substring (0, dot);
  558. if (real_id == null || used.ContainsKey (real_id))
  559. continue;
  560. used.Add (real_id, real_id);
  561. Control ctrl = FindControl (real_id);
  562. if (ctrl != null){
  563. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  564. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  565. if (pbdh == null) {
  566. if (pbeh != null)
  567. RegisterRequiresRaiseEvent (pbeh);
  568. continue;
  569. }
  570. if (pbdh.LoadPostData (real_id, data) == true) {
  571. if (requiresPostDataChanged == null)
  572. requiresPostDataChanged = new ArrayList ();
  573. requiresPostDataChanged.Add (pbdh);
  574. if (_requiresPostBackCopy != null)
  575. _requiresPostBackCopy.Remove (ctrl.UniqueID);
  576. }
  577. } else if (!second) {
  578. if (secondPostData == null)
  579. secondPostData = new NameValueCollection ();
  580. secondPostData.Add (real_id, data [id]);
  581. }
  582. }
  583. ArrayList list1 = null;
  584. if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
  585. string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
  586. foreach (string id in handlers) {
  587. IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
  588. if (pbdh != null) {
  589. _requiresPostBackCopy.Remove (id);
  590. if (pbdh.LoadPostData (id, data)) {
  591. if (requiresPostDataChanged == null)
  592. requiresPostDataChanged = new ArrayList ();
  593. requiresPostDataChanged.Add (pbdh);
  594. }
  595. } else if (second) {
  596. if (list1 == null)
  597. list1 = new ArrayList ();
  598. list1.Add (id);
  599. }
  600. }
  601. _requiresPostBack = list1;
  602. }
  603. }
  604. [EditorBrowsable (EditorBrowsableState.Never)]
  605. public void ProcessRequest (HttpContext context)
  606. {
  607. _context = context;
  608. if (clientTarget != null)
  609. Request.ClientTarget = clientTarget;
  610. WireupAutomaticEvents ();
  611. //-- Control execution lifecycle in the docs
  612. // Save culture information because it can be modified in FrameworkInitialize()
  613. CultureInfo culture = Thread.CurrentThread.CurrentCulture;
  614. CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
  615. FrameworkInitialize ();
  616. context.ErrorPage = _errorPage;
  617. try {
  618. InternalProcessRequest ();
  619. } finally {
  620. try {
  621. UnloadRecursive (true);
  622. } catch {}
  623. Thread.CurrentThread.CurrentCulture = culture;
  624. Thread.CurrentThread.CurrentUICulture = uiculture;
  625. }
  626. }
  627. void InternalProcessRequest ()
  628. {
  629. _requestValueCollection = this.DeterminePostBackMode();
  630. Trace.Write ("aspx.page", "Begin Init");
  631. InitRecursive (null);
  632. Trace.Write ("aspx.page", "End Init");
  633. renderingForm = false;
  634. if (IsPostBack) {
  635. Trace.Write ("aspx.page", "Begin LoadViewState");
  636. LoadPageViewState ();
  637. Trace.Write ("aspx.page", "End LoadViewState");
  638. Trace.Write ("aspx.page", "Begin ProcessPostData");
  639. ProcessPostData (_requestValueCollection, false);
  640. Trace.Write ("aspx.page", "End ProcessPostData");
  641. }
  642. LoadRecursive ();
  643. if (IsPostBack) {
  644. Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
  645. ProcessPostData (secondPostData, true);
  646. Trace.Write ("aspx.page", "End ProcessPostData Second Try");
  647. Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
  648. RaiseChangedEvents ();
  649. Trace.Write ("aspx.page", "End Raise ChangedEvents");
  650. Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
  651. RaisePostBackEvents ();
  652. Trace.Write ("aspx.page", "End Raise PostBackEvent");
  653. }
  654. Trace.Write ("aspx.page", "Begin PreRender");
  655. PreRenderRecursiveInternal ();
  656. Trace.Write ("aspx.page", "End PreRender");
  657. Trace.Write ("aspx.page", "Begin SaveViewState");
  658. SavePageViewState ();
  659. Trace.Write ("aspx.page", "End SaveViewState");
  660. //--
  661. Trace.Write ("aspx.page", "Begin Render");
  662. HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
  663. RenderControl (output);
  664. Trace.Write ("aspx.page", "End Render");
  665. RenderTrace (output);
  666. }
  667. private void RenderTrace (HtmlTextWriter output)
  668. {
  669. TraceManager traceManager = HttpRuntime.TraceManager;
  670. if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
  671. return;
  672. Trace.SaveData ();
  673. if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput)
  674. return;
  675. if (!traceManager.LocalOnly || Context.Request.IsLocal)
  676. Trace.Render (output);
  677. }
  678. internal void RaisePostBackEvents ()
  679. {
  680. if (requiresRaiseEvent != null) {
  681. RaisePostBackEvent (requiresRaiseEvent, null);
  682. return;
  683. }
  684. NameValueCollection postdata = _requestValueCollection;
  685. if (postdata == null)
  686. return;
  687. string eventTarget = postdata [postEventSourceID];
  688. if (eventTarget == null || eventTarget.Length == 0) {
  689. Validate ();
  690. return;
  691. }
  692. IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
  693. if (target == null)
  694. return;
  695. string eventArgument = postdata [postEventArgumentID];
  696. RaisePostBackEvent (target, eventArgument);
  697. }
  698. internal void RaiseChangedEvents ()
  699. {
  700. if (requiresPostDataChanged == null)
  701. return;
  702. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  703. ipdh.RaisePostDataChangedEvent ();
  704. requiresPostDataChanged = null;
  705. }
  706. [EditorBrowsable (EditorBrowsableState.Advanced)]
  707. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  708. {
  709. sourceControl.RaisePostBackEvent (eventArgument);
  710. }
  711. [EditorBrowsable (EditorBrowsableState.Advanced)]
  712. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  713. {
  714. if (registeredArrayDeclares == null)
  715. registeredArrayDeclares = new Hashtable();
  716. if (!registeredArrayDeclares.ContainsKey (arrayName))
  717. registeredArrayDeclares.Add (arrayName, new ArrayList());
  718. ((ArrayList) registeredArrayDeclares[arrayName]).Add(arrayValue);
  719. }
  720. [EditorBrowsable (EditorBrowsableState.Advanced)]
  721. public virtual void RegisterClientScriptBlock (string key, string script)
  722. {
  723. if (IsClientScriptBlockRegistered (key))
  724. return;
  725. if (clientScriptBlocks == null)
  726. clientScriptBlocks = new Hashtable ();
  727. clientScriptBlocks.Add (key, script);
  728. }
  729. [EditorBrowsable (EditorBrowsableState.Advanced)]
  730. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  731. {
  732. if (hiddenFields == null)
  733. hiddenFields = new Hashtable ();
  734. if (!hiddenFields.ContainsKey (hiddenFieldName))
  735. hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
  736. }
  737. [MonoTODO("Used in HtmlForm")]
  738. internal void RegisterClientScriptFile (string a, string b, string c)
  739. {
  740. throw new NotImplementedException ();
  741. }
  742. [EditorBrowsable (EditorBrowsableState.Advanced)]
  743. public void RegisterOnSubmitStatement (string key, string script)
  744. {
  745. if (submitStatements == null)
  746. submitStatements = new Hashtable ();
  747. if (submitStatements.ContainsKey (key))
  748. return;
  749. submitStatements.Add (key, script);
  750. }
  751. [EditorBrowsable (EditorBrowsableState.Advanced)]
  752. public void RegisterRequiresPostBack (Control control)
  753. {
  754. if (_requiresPostBack == null)
  755. _requiresPostBack = new ArrayList ();
  756. _requiresPostBack.Add (control.UniqueID);
  757. }
  758. [EditorBrowsable (EditorBrowsableState.Advanced)]
  759. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  760. {
  761. requiresRaiseEvent = control;
  762. }
  763. [EditorBrowsable (EditorBrowsableState.Advanced)]
  764. public virtual void RegisterStartupScript (string key, string script)
  765. {
  766. if (IsStartupScriptRegistered (key))
  767. return;
  768. if (startupScriptBlocks == null)
  769. startupScriptBlocks = new Hashtable ();
  770. startupScriptBlocks.Add (key, script);
  771. }
  772. [EditorBrowsable (EditorBrowsableState.Advanced)]
  773. public void RegisterViewStateHandler ()
  774. {
  775. handleViewState = true;
  776. }
  777. [EditorBrowsable (EditorBrowsableState.Advanced)]
  778. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  779. {
  780. _savedViewState = viewState;
  781. }
  782. [EditorBrowsable (EditorBrowsableState.Advanced)]
  783. protected virtual object LoadPageStateFromPersistenceMedium ()
  784. {
  785. NameValueCollection postdata = _requestValueCollection;
  786. string view_state;
  787. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  788. return null;
  789. _savedViewState = null;
  790. LosFormatter fmt = new LosFormatter ();
  791. try {
  792. _savedViewState = fmt.Deserialize (view_state);
  793. } catch (Exception e) {
  794. throw new HttpException ("Error restoring page viewstate.\n", e);
  795. }
  796. return _savedViewState;
  797. }
  798. internal void LoadPageViewState()
  799. {
  800. object sState = LoadPageStateFromPersistenceMedium ();
  801. if (sState != null) {
  802. Pair pair = (Pair) sState;
  803. LoadViewStateRecursive (pair.First);
  804. _requiresPostBack = pair.Second as ArrayList;
  805. }
  806. }
  807. internal void SavePageViewState ()
  808. {
  809. if (!handleViewState)
  810. return;
  811. Pair pair = new Pair ();
  812. pair.First = SaveViewStateRecursive ();
  813. if (_requiresPostBack != null && _requiresPostBack.Count > 0)
  814. pair.Second = _requiresPostBack;
  815. if (pair.First == null && pair.Second == null)
  816. pair = null;
  817. SavePageStateToPersistenceMedium (pair);
  818. }
  819. public virtual void Validate ()
  820. {
  821. if (_validators == null || _validators.Count == 0){
  822. _isValid = true;
  823. return;
  824. }
  825. bool all_valid = true;
  826. foreach (IValidator v in _validators){
  827. v.Validate ();
  828. if (v.IsValid == false)
  829. all_valid = false;
  830. }
  831. if (all_valid)
  832. _isValid = true;
  833. }
  834. [EditorBrowsable (EditorBrowsableState.Advanced)]
  835. public virtual void VerifyRenderingInServerForm (Control control)
  836. {
  837. if (!renderingForm)
  838. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  839. "' must be rendered within a HtmlForm");
  840. }
  841. #endregion
  842. #if NET_2_0
  843. public string GetWebResourceUrl(Type type, string resourceName)
  844. {
  845. if (type == null)
  846. throw new ArgumentNullException ("type");
  847. if (resourceName == null || resourceName.Length == 0)
  848. throw new ArgumentNullException ("type");
  849. return System.Web.Handlers.AssemblyResourceLoader.GetResourceUrl (type, resourceName);
  850. }
  851. Stack dataItemCtx;
  852. internal void PushDataItemContext (object o)
  853. {
  854. if (dataItemCtx == null)
  855. dataItemCtx = new Stack ();
  856. dataItemCtx.Push (o);
  857. }
  858. internal void PopDataItemContext ()
  859. {
  860. if (dataItemCtx == null)
  861. throw new InvalidOperationException ();
  862. dataItemCtx.Pop ();
  863. }
  864. internal object CurrentDataItem {
  865. get {
  866. if (dataItemCtx == null)
  867. throw new InvalidOperationException ("No data item");
  868. return dataItemCtx.Peek ();
  869. }
  870. }
  871. protected object Eval (string expression)
  872. {
  873. return DataBinder.Eval (CurrentDataItem, expression);
  874. }
  875. protected object Eval (string expression, string format)
  876. {
  877. return DataBinder.Eval (CurrentDataItem, expression, format);
  878. }
  879. protected object XPath (string xpathexpression)
  880. {
  881. return XPathBinder.Eval (CurrentDataItem, xpathexpression);
  882. }
  883. protected object XPath (string xpathexpression, string format)
  884. {
  885. return XPathBinder.Eval (CurrentDataItem, xpathexpression, format);
  886. }
  887. protected IEnumerable XPathSelect (string xpathexpression)
  888. {
  889. return XPathBinder.Select (CurrentDataItem, xpathexpression);
  890. }
  891. #endif
  892. }
  893. }