public static object ConvertEditedValue(this IFieldFormattingOptions formattingOptions, string value)
        {
            // Not a surprise anymore...
            if (formattingOptions == null)
            {
                throw new NullReferenceException();
            }

            if (String.IsNullOrEmpty(value))
            {
                if (formattingOptions.ConvertEmptyStringToNull)
                {
                    return(null);
                }
            }
            else
            {
                string nullDisplayText = formattingOptions.NullDisplayText;
                if (!String.IsNullOrEmpty(nullDisplayText) && String.Compare(value, nullDisplayText, StringComparison.Ordinal) == 0)
                {
                    return(null);
                }
            }

            return(value);
        }
        /// <summary>
        /// Return either the input value or null based on ConvertEmptyStringToNull and NullDisplayText
        /// </summary>
        /// <param name="formattingOptions">the formatting options object</param>
        /// <param name="value">The input value</param>
        /// <returns>The converted value</returns>
        public static object ConvertEditedValue(this IFieldFormattingOptions formattingOptions, string value)
        {
            // If it's an empty string and ConvertEmptyStringToNull is set, make it null
            if (String.IsNullOrEmpty(value) && formattingOptions.ConvertEmptyStringToNull)
            {
                return(null);
            }

            // If it's the NullDisplayText, return null
            string nullDisplayText = formattingOptions.NullDisplayText;

            if (value == nullDisplayText && !String.IsNullOrEmpty(nullDisplayText))
            {
                return(null);
            }

            // Otherwise, return it unchanged
            return(value);
        }
        /// <summary>
        /// Similar to FormatValue, but the string is to be used when the field is in edit mode
        /// </summary>
        public static string FormatEditValue(this IFieldFormattingOptions formattingOptions, object fieldValue)
        {
            string valueString;

            // Apply the format string to it if that flag is set.  Otherwise use it as is.
            if (formattingOptions.ApplyFormatInEditMode)
            {
                valueString = formattingOptions.FormatValue(fieldValue);
            }
            else
            {
                valueString = (fieldValue != null) ? fieldValue.ToString() : String.Empty;
            }

            // Trim any trailing spaces as they cause unwanted behavior (since we limit the input length and the
            // spaces cause the limit to be reach prematurely)
            valueString = valueString.TrimEnd();

            return(valueString);
        }
        /// <summary>
        /// Apply potential HTML encoding and formatting to a string that needs to be displayed
        /// This logic is mostly copied from BoundField.FormatDataValue, but omits the old Whidbey behavior path
        /// </summary>
        /// <param name="fieldValue">The value that should be formatted</param>
        /// <param name="formattingOptions">The IFieldFormattingOptions to use. This is useful when using options different from the column's</param>
        /// <returns>the formatted value</returns>
        public static string FormatValue(this IFieldFormattingOptions formattingOptions, object fieldValue)
        {
            string formattedValue = String.Empty;

            if (fieldValue != null)
            {
                string dataValueString       = fieldValue.ToString();
                string formatting            = formattingOptions.DataFormatString;
                int    dataValueStringLength = dataValueString.Length;

                // If the result is still empty and ConvertEmptyStringToNull=true, replace the value with the NullDisplayText
                if (dataValueStringLength == 0 && formattingOptions.ConvertEmptyStringToNull)
                {
                    dataValueString = formattingOptions.NullDisplayText;
                }
                else
                {
                    // If there's a format string, apply it to the raw data value
                    // If there's no format string, then dataValueString already has the right value
                    if (!String.IsNullOrEmpty(formatting))
                    {
                        dataValueString = String.Format(CultureInfo.CurrentCulture, formatting, fieldValue);
                    }

                    // Optionally HTML encode the value (including the format string, if any was applied)
                    if (!String.IsNullOrEmpty(dataValueString) && formattingOptions.HtmlEncode)
                    {
                        dataValueString = HttpUtility.HtmlEncode(dataValueString);
                    }
                }

                formattedValue = dataValueString;
            }
            else
            {
                formattedValue = formattingOptions.NullDisplayText;
            }

            return(formattedValue);
        }
 public static string FormatValue(this IFieldFormattingOptions formattingOptions, object fieldValue)
 {
     throw new NotImplementedException();
 }