PersistentUserForm.aspx.cs
上传用户:husern
上传日期:2022-03-24
资源大小:534k
文件大小:5k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. // -- FILE ------------------------------------------------------------------
  2. // name       : PersistentUserForm.cs
  3. // project    : Itenso Web User Forms
  4. // created    : Jani Giannoudis - 2008.10.30
  5. // language   : c#
  6. // environment: .NET 2.0
  7. // copyright  : (c) 2008 by Itenso GmbH, Switzerland
  8. // --------------------------------------------------------------------------
  9. using System;
  10. using System.IO;
  11. using System.Web.UI;
  12. using System.Web.UI.WebControls;
  13. using Itenso.WebUserForms.Controls;
  14. using Itenso.WebUserForms.Data.Form;
  15. using Itenso.WebUserForms.Runtime;
  16. // --------------------------------------------------------------------------
  17. public partial class PersistentPage : System.Web.UI.Page 
  18. {
  19. // ------------------------------------------------------------------------
  20. protected override void OnInit( EventArgs e )
  21. {
  22. base.OnInit( e );
  23. MasterPage masterPage = Master as MasterPage;
  24. if ( masterPage != null )
  25. {
  26. masterPage.ShowRuntimeHeaderInfo = true;
  27. }
  28. } // OnInit
  29. // ------------------------------------------------------------------------
  30. protected override void OnLoad( EventArgs e )
  31. {
  32. // load form control
  33. this.userForm = new UserFormLoader( "~/UserForms/PersistentUserForm.ascx" ).Load();
  34. this.commandManager = new UserFormCommandManager( this.userForm );
  35. this.commandManager.Command += new CommandEventHandler( FormCommand );
  36. FormPlaceHolder.Controls.Add( this.userForm );
  37. if ( !Page.IsPostBack )
  38. {
  39. LoadFormData();
  40. }
  41. base.OnLoad( e );
  42. } // OnLoad
  43. // ------------------------------------------------------------------------
  44. private void UpdateCommands( IForm form )
  45. {
  46. this.commandManager.EnableCommand( commandFormSave, !form.IsLocked );
  47. this.commandManager.EnableCommand( commandFormLock, !form.IsLocked );
  48. this.commandManager.EnableCommand( commandFormUnlock, form.IsLocked );
  49. } // UpdateCommands
  50. // ------------------------------------------------------------------------
  51. private void LoadFormData()
  52. {
  53. // file check
  54. string fileName = MapPath( virtualFileName );
  55. if ( !File.Exists( fileName ) )
  56. {
  57. return;
  58. }
  59. // load form data
  60. using ( StreamReader streamReader = new StreamReader( fileName ) )
  61. {
  62. IForm form = FormXml.Instance.Load( streamReader );
  63. UserFormAdapter.ApplyForm( this.userForm, form );
  64. UpdateCommands( form );
  65. }
  66. } // LoadFormData
  67. // ------------------------------------------------------------------------
  68. private void SaveFormData()
  69. {
  70. IForm form = UserFormAdapter.ExtractForm( this.userForm );
  71. string fileName = MapPath( virtualFileName );
  72. if ( File.Exists( fileName ) ) // update existing form
  73. {
  74. File.Delete( fileName );
  75. form.MarkUpdated( DateTime.Now, "DemoUser" );
  76. }
  77. else // new form
  78. {
  79. form.FormId = "1";
  80. form.SetCreated( DateTime.Now, "DemoUser" );
  81. }
  82. // ensure directory
  83. string directory = new FileInfo( fileName ).DirectoryName;
  84. if ( !Directory.Exists( directory ) )
  85. {
  86. Directory.CreateDirectory( directory );
  87. }
  88. // save form data
  89. using ( StreamWriter streamWriter = new StreamWriter( fileName ) )
  90. {
  91. FormXml.Instance.Save( form, streamWriter );
  92. UserFormAdapter.ApplyForm( this.userForm, form );
  93. }
  94. UpdateCommands( form );
  95. } // SaveFormData
  96. // ------------------------------------------------------------------------
  97. private void ChangeFormLock( bool isLocked )
  98. {
  99. IForm form = UserFormAdapter.ExtractForm( this.userForm );
  100. form.IsLocked = isLocked;
  101. UpdateCommands( form );
  102. } // ChangeFormLock
  103. // ------------------------------------------------------------------------
  104. private void FormCommand( object sender, CommandEventArgs e )
  105. {
  106. switch ( e.CommandName.ToLower() )
  107. {
  108. case commandFormSave:
  109. SaveFormData();
  110. break;
  111. case commandFormLoad:
  112. LoadFormData();
  113. break;
  114. case commandFormLock:
  115. ChangeFormLock( true );
  116. break;
  117. case commandFormUnlock:
  118. ChangeFormLock( false );
  119. break;
  120. default:
  121. // unknown command handling
  122. break;
  123. }
  124. } // FormCommand
  125. // ------------------------------------------------------------------------
  126. // members
  127. private UserControl userForm;
  128. private UserFormCommandManager commandManager;
  129. private const string virtualFileName = "~/Data/PersistentUserForm.xml";
  130. private const string commandFormSave = "formsave";
  131. private const string commandFormLoad = "formload";
  132. private const string commandFormLock = "formlock";
  133. private const string commandFormUnlock = "formunlock";
  134. } // class PersistentPage
  135. // -- EOF -------------------------------------------------------------------