예제 #1
0
        void ControlTreeDataLoader.LoadData()
        {
            var isTextarea = rows > 1;

            textBox    = new WebControl(isTextarea ? HtmlTextWriterTag.Textarea : HtmlTextWriterTag.Input);
            PreRender += delegate {
                if (!isTextarea)
                {
                    textBox.Attributes.Add("type", masksCharacters ? "password" : "text");
                }
                textBox.Attributes.Add("name", UniqueID);
                if (isTextarea)
                {
                    textBox.Attributes.Add("rows", rows.ToString());
                }
                if (maxLength.HasValue)
                {
                    textBox.Attributes.Add("maxlength", maxLength.Value.ToString());
                }
                if (readOnly)
                {
                    textBox.Attributes.Add("readonly", "readonly");
                }
                if (disableBrowserAutoComplete || autoCompleteService != null)
                {
                    textBox.Attributes.Add("autocomplete", "off");
                }
                if (suggestSpellCheck.HasValue)
                {
                    textBox.Attributes.Add("spellcheck", suggestSpellCheck.Value.ToString().ToLower());
                }

                var value            = formValue.GetValue(AppRequestState.Instance.EwfPageRequestState.PostBackValues);
                var valueOrWatermark = watermarkText.Any() && !value.Any() ? watermarkText : value;
                if (isTextarea)
                {
                    textBox.Controls.Add(new Literal {
                        Text = HttpUtility.HtmlEncode(GetTextareaValue(valueOrWatermark))
                    });
                }
                else if (!masksCharacters)
                {
                    textBox.Attributes.Add("value", valueOrWatermark);
                }
            };
            Controls.Add(textBox);

            if (watermarkText.Any())
            {
                textBox.AddJavaScriptEventScript(JsWritingMethods.onfocus, "if( value == '" + watermarkText + "' ) value = ''");
                textBox.AddJavaScriptEventScript(JsWritingMethods.onblur, "if( value == '' ) value = '" + watermarkText + "'");
                EwfPage.Instance.ClientScript.RegisterOnSubmitStatement(
                    GetType(),
                    UniqueID + "watermark",
                    "$( '#" + textBox.ClientID + "' ).filter( function() { return this.value == '" + watermarkText + "'; } ).val( '' )");
            }

            if (!isTextarea || autoPostBack || (autoCompleteService != null && autoCompleteOption != AutoCompleteOption.NoPostBack))
            {
                action.AddToPageIfNecessary();
            }
            if (!isTextarea)
            {
                PreRender +=
                    delegate {
                    SubmitButton.EnsureImplicitSubmissionAction(
                        this,
                        action,
                        autoPostBack || (autoCompleteService != null && autoCompleteOption == AutoCompleteOption.PostBackOnTextChangeAndItemSelect));
                }
            }
            ;

            if (autoPostBack || (autoCompleteService != null && autoCompleteOption == AutoCompleteOption.PostBackOnTextChangeAndItemSelect))
            {
                PreRender += delegate {
                    // Use setTimeout to prevent keypress and change from *both* triggering post-backs at the same time when Enter is pressed after a text change.
                    textBox.AddJavaScriptEventScript(JsWritingMethods.onchange, "setTimeout( function() { " + action.GetJsStatements() + " }, 0 )");
                }
            }
            ;

            if (ToolTip != null || ToolTipControl != null)
            {
                new ToolTip(ToolTipControl ?? EnterpriseWebFramework.Controls.ToolTip.GetToolTipTextControl(ToolTip), textBox);
            }
        }

        string ControlWithJsInitLogic.GetJsInitStatements()
        {
            var script = new StringBuilder();

            if (watermarkText.Any())
            {
                var restorationStatement = "$( '#" + textBox.ClientID + "' ).filter( function() { return this.value == ''; } ).val( '" + watermarkText + "' );";

                // The first line is for bfcache browsers; the second is for all others. See http://stackoverflow.com/q/1195440/35349.
                script.Append("$( window ).on( 'pagehide', function() { " + restorationStatement + " } );");
                script.Append(restorationStatement);
            }

            if (autoCompleteService != null)
            {
                const int delay         = 250;         // Default delay is 300 ms.
                const int minCharacters = 3;

                var autocompleteOptions = new List <Tuple <string, string> >();
                autocompleteOptions.Add(Tuple.Create("delay", delay.ToString()));
                autocompleteOptions.Add(Tuple.Create("minLength", minCharacters.ToString()));
                autocompleteOptions.Add(Tuple.Create("source", "'" + autoCompleteService.GetUrl() + "'"));

                if (autoCompleteOption != AutoCompleteOption.NoPostBack)
                {
                    var handler = "function( event, ui ) {{ $( '#{0}' ).val( ui.item.value ); {1} return false; }}".FormatWith(textBox.ClientID, action.GetJsStatements());
                    autocompleteOptions.Add(Tuple.Create("select", handler));
                }

                script.Append(
                    @"$( '#" + textBox.ClientID +
                    "' ).autocomplete( {{ {0} }} );".FormatWith(
                        autocompleteOptions.Select(o => "{0}: {1}".FormatWith(o.Item1, o.Item2)).GetCommaDelimitedStringFromCollection()));
            }

            return(script.ToString());
        }

        FormValue FormValueControl.FormValue {
            get { return(formValue); }
        }