Beispiel #1
0
        private void RegisterSmartPageInitializationScript()
        {
            var htmlForm = _page.Form;

            if (htmlForm == null)
            {
                throw new InvalidOperationException("SmartPage requires an HtmlForm control on the page.");
            }

            string abortMessage = GetAbortMessage();
            string statusIsSubmittingMessage = GetStatusIsSubmittingMessage();

            string checkFormStateFunction = "null";

            if (!string.IsNullOrEmpty(_checkFormStateFunction))
            {
                checkFormStateFunction = "'" + _checkFormStateFunction + "'";
            }

            string smartScrollingFieldID = "null";
            string smartFocusFieldID     = "null";

            ISmartNavigablePage smartNavigablePage = _page as ISmartNavigablePage;

            if (smartNavigablePage != null)
            {
                if (smartNavigablePage.IsSmartScrollingEnabled)
                {
                    smartScrollingFieldID = "'" + c_smartScrollingID + "'";
                }
                if (smartNavigablePage.IsSmartFocusingEnabled)
                {
                    smartFocusFieldID = "'" + c_smartFocusID + "'";
                }
            }

            string isDirtyStateTrackingEnabled = "false";
            string isDirty = "false";

            StringBuilder initScript = new StringBuilder(500);

            initScript.AppendLine("function SmartPage_Initialize ()");
            initScript.AppendLine("{");

            const string eventHandlersArray = "eventHandlers";

            initScript.Append("  var ").Append(eventHandlersArray).AppendLine(" = new Array();");
            FormatPopulateEventHandlersArrayClientScript(initScript, eventHandlersArray);
            initScript.AppendLine();

            const string trackedControlsArray = "trackedControls";

            initScript.Append("  var ").Append(trackedControlsArray).AppendLine(" = new Array();");
            if (_page.IsDirtyStateTrackingEnabled)
            {
                isDirtyStateTrackingEnabled = "true";
                if (_page.EvaluateDirtyState())
                {
                    isDirty = "true";
                }
                else
                {
                    FormatPopulateTrackedControlsArrayClientScript(initScript, trackedControlsArray);
                }
            }
            initScript.AppendLine();

            const string synchronousPostBackCommandsArray = "synchronousPostBackCommands";

            initScript.Append("  var ").Append(synchronousPostBackCommandsArray).AppendLine(" = new Array();");
            FormatPopulateSynchronousPostBackCommandsArrayClientScript(initScript, synchronousPostBackCommandsArray);
            initScript.AppendLine();

            initScript.AppendLine("  if (SmartPage_Context.Instance == null)");
            initScript.AppendLine("  {");

            initScript.AppendLine();

            initScript.AppendLine("    SmartPage_Context.Instance = new SmartPage_Context (");
            initScript.Append("        '").Append(htmlForm.ClientID).AppendLine("',");
            initScript.Append("        ").Append(isDirtyStateTrackingEnabled).AppendLine(",");
            initScript.Append("        ").Append(abortMessage).AppendLine(",");
            initScript.Append("        ").Append(statusIsSubmittingMessage).AppendLine(",");
            initScript.Append("        ").Append(smartScrollingFieldID).AppendLine(",");
            initScript.Append("        ").Append(smartFocusFieldID).AppendLine(",");
            initScript.Append("        ").Append(checkFormStateFunction).AppendLine(");");

            initScript.AppendLine("  }");
            initScript.AppendLine();

            initScript.Append("  SmartPage_Context.Instance.set_EventHandlers (").Append(eventHandlersArray).AppendLine(");");
            initScript.Append("  SmartPage_Context.Instance.set_TrackedIDs (").Append(trackedControlsArray).AppendLine(");");
            initScript.Append("  SmartPage_Context.Instance.set_SynchronousPostBackCommands (").Append(synchronousPostBackCommandsArray).AppendLine(");");
            initScript.AppendLine("}");
            initScript.AppendLine();
            initScript.AppendLine("SmartPage_Initialize ();");
            initScript.AppendLine();

            _page.ClientScript.RegisterClientScriptBlock(_page, typeof(SmartPageInfo), "smartPageInitialize", initScript.ToString());

            string isAsynchronous = "false";

            if (IsInAsyncPostBack)
            {
                isAsynchronous = "true";
            }
            _page.ClientScript.RegisterStartupScriptBlock(_page, typeof(SmartPageInfo), "smartPageStartUp", "SmartPage_OnStartUp (" + isAsynchronous + ", " + isDirty + ");");

            // Ensure the __doPostBack function and the __EventTarget and __EventArgument hidden fields on the rendered page
            _page.ClientScript.GetPostBackEventReference(new PostBackOptions(_page.WrappedInstance)
            {
                ClientSubmit = true
            }, false);
        }