Example #1
0
        /// <summary>
        /// Renders out field with honeypot security check enabled
        /// </summary>
        /// <param name="helper">HtmlHelper which will be extended</param>
        /// <param name="name">Name of field. Should match model field of string type</param>
        /// <param name="value">Value of the field</param>
        /// <param name="css">CSS class to be applied to input field</param>
        /// <returns>Returns render out MvcHtmlString for displaying on the View</returns>
        public static MvcHtmlString HoneyPotField(this HtmlHelper helper, string name, object value, string inputCss = null, InputType fieldType = InputType.Text, string honeypotCss = null, InputType honeypotType = InputType.Hidden)
        {
            StringBuilder sbControlHtml = new StringBuilder();

            using (StringWriter stringWriter = new StringWriter())
            {
                using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
                {
                    HtmlInputText hashedField = new HtmlInputText(fieldType.ToString().ToLower());
                    string        hashedName  = GetHashedPropertyName(name);
                    hashedField.Value = value != null?value.ToString() : string.Empty;

                    hashedField.ID   = hashedName;
                    hashedField.Name = hashedName;
                    if (!string.IsNullOrWhiteSpace(inputCss))
                    {
                        hashedField.Attributes["class"] = inputCss;
                    }
                    hashedField.RenderControl(htmlWriter);


                    HtmlInputText hiddenField = new HtmlInputText(honeypotType.ToString().ToLower());
                    hiddenField.Value = string.Empty;
                    hiddenField.ID    = name;
                    hiddenField.Name  = name;
                    if (!string.IsNullOrWhiteSpace(honeypotCss))
                    {
                        hiddenField.Attributes["class"] = honeypotCss;
                    }
                    hiddenField.RenderControl(htmlWriter);
                    sbControlHtml.Append(stringWriter.ToString());
                }
            }
            return(new MvcHtmlString(sbControlHtml.ToString()));
        }
Example #2
0
        /// <summary>
        /// Renders out input field with honeypot security check enabled
        /// </summary>
        /// <param name="helper">HtmlHelper which will be extended</param>
        /// <param name="name">Name of field. Should match model field of string type</param>
        /// <param name="value">Value of the field</param>
        /// <param name="addLabel">true if the the input has a label. Default: true</param>
        /// <param name="labelContent">Label content. Default : name param</param>
        /// <param name="inputCss">CSS class to be applied to the visible input field. Default: null</param>
        /// <param name="fieldType">Enum represents the visible input type.Default: InputType.Text</param>
        /// <param name="honeypotCss">CSS class to be applied to the trap input field</param>
        /// <param name="honeypotType">Enum represents the input type. Default: InputType.Hidden</param>
        /// <returns>Returns render out MvcHtmlString for displaying on the View</returns>
        public static MvcHtmlString HoneyPotField(this HtmlHelper helper, string name, object value, bool addLabel = true,
                                                  string labelContent = null, string inputCss = null, InputType fieldType = InputType.Text, string honeypotCss = null, InputType honeypotType = InputType.Hidden)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            StringBuilder sbControlHtml = new StringBuilder();

            using (StringWriter stringWriter = new StringWriter())
            {
                using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
                {
                    HtmlInputText hashedField = new HtmlInputText(fieldType.ToString().ToLower());
                    string        hashedName  = GetHashedPropertyName(name);
                    hashedField.Value = value != null?value.ToString() : String.Empty;

                    hashedField.ID   = hashedName;
                    hashedField.Name = hashedName;
                    if (!String.IsNullOrWhiteSpace(inputCss))
                    {
                        hashedField.Attributes["class"] = inputCss;
                    }
                    if (addLabel)
                    {
                        HtmlGenericControl label = new HtmlGenericControl("label");
                        if (String.IsNullOrEmpty(labelContent))
                        {
                            label.InnerText = name;
                        }
                        else
                        {
                            label.InnerText = labelContent;
                        }
                        label.Attributes.Add("for", hashedName);
                        label.Attributes.Add("style", "margin-right: 15px");
                        label.RenderControl(htmlWriter);
                    }
                    hashedField.RenderControl(htmlWriter);


                    HtmlInputText hiddenField = new HtmlInputText(honeypotType.ToString().ToLower());
                    hiddenField.Value = String.Empty;
                    hiddenField.ID    = name;
                    hiddenField.Name  = name;
                    if (!String.IsNullOrWhiteSpace(honeypotCss))
                    {
                        hiddenField.Attributes["class"] = honeypotCss;
                    }
                    hiddenField.RenderControl(htmlWriter);
                    sbControlHtml.Append(stringWriter.ToString());
                }
            }
            return(new MvcHtmlString(sbControlHtml.ToString()));
        }
Example #3
0
        /// <summary>
        /// Renders out field with honeypot security check enabled
        /// </summary>
        /// <param name="helper">HtmlHelper which will be extended</param>
        /// <param name="name">Name of field. Should match model field of string type</param>
        /// <param name="value">Value of the field</param>
        /// <param name="htmlAttributes">Attributes to add to the HTML tag</param>
        /// <param name="fieldType">The field type</param>
        /// <param name="honeypotCss">The CSS class applied to the honeypot HTML tag</param>
        /// <param name="honeypotType">The type of input of the honeypot</param>
        /// <returns>Returns render out MvcHtmlString for displaying on the View</returns>
        public static MvcHtmlString HoneyPotField(this HtmlHelper helper, string name, object value, dynamic htmlAttributes = null, InputType fieldType = InputType.Text, string honeypotCss = null, InputType honeypotType = InputType.Hidden)
        {
            StringBuilder sbControlHtml = new StringBuilder();

            using (StringWriter stringWriter = new StringWriter())
            {
                using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
                {
                    HtmlInputText hashedField = new HtmlInputText(fieldType.ToString().ToLower());
                    string        hashedName  = GetHashedPropertyName(name);
                    hashedField.Value = value != null?value.ToString() : string.Empty;

                    hashedField.ID   = hashedName;
                    hashedField.Name = hashedName;
                    if (htmlAttributes != null)
                    {
                        foreach (var property in htmlAttributes.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                        {
                            var attributeName  = property.Name.Replace('_', '-');
                            var attributeValue = property.GetValue(htmlAttributes, null);
                            hashedField.Attributes[attributeName] = attributeValue;
                        }
                    }
                    hashedField.RenderControl(htmlWriter);


                    HtmlInputText hiddenField = new HtmlInputText(honeypotType.ToString().ToLower());
                    hiddenField.Value = string.Empty;
                    hiddenField.ID    = name;
                    hiddenField.Name  = name;
                    if (!string.IsNullOrWhiteSpace(honeypotCss))
                    {
                        hiddenField.Attributes["class"] = honeypotCss;
                    }
                    hiddenField.RenderControl(htmlWriter);
                    sbControlHtml.Append(stringWriter.ToString());
                }
            }
            return(new MvcHtmlString(sbControlHtml.ToString()));
        }
Example #4
0
        private string GeneratePropertyControl(ProductProperty item, string propertyValue)
        {
            using (var sw = new StringWriter())
            {
                using (var writer = new HtmlTextWriter(sw))
                {
                    if (item.TypeCode == ProductPropertyType.CurrencyField)
                    {
                        var input = new HtmlInputText();
                        input.ID = "ProductTypeProperty" + item.Id;
                        if (propertyValue != null)
                        {
                            input.Value = propertyValue;
                        }
                        else
                        {
                            input.Value = item.DefaultValue;
                        }
                        input.RenderControl(writer);
                    }
                    else if (item.TypeCode == ProductPropertyType.DateField)
                    {
                        var input = new HtmlInputText();
                        input.ID = "ProductTypeProperty" + item.Id;
                        if (propertyValue != null)
                        {
                            input.Value = propertyValue;
                        }
                        else
                        {
                            input.Value = item.DefaultValue;
                        }
                        input.RenderControl(writer);
                    }
                    else if (item.TypeCode == ProductPropertyType.HyperLink)
                    {
                        var input = new HtmlInputText();
                        input.ID = "ProductTypeProperty" + item.Id;
                        if (propertyValue != null)
                        {
                            input.Value = propertyValue;
                        }
                        else
                        {
                            input.Value = item.DefaultValue;
                        }
                        input.RenderControl(writer);
                    }
                    else if (item.TypeCode == ProductPropertyType.MultipleChoiceField)
                    {
                        var input = new HtmlSelect();
                        input.ID = "ProductTypeProperty" + item.Id;
                        foreach (var choice in item.Choices)
                        {
                            var li = new ListItem(choice.DisplayName, choice.Id.ToString());
                            input.Items.Add(li);
                        }

                        if (propertyValue != null)
                        {
                            input.Value = propertyValue;
                        }
                        else
                        {
                            input.Value = item.DefaultValue;
                        }
                        input.RenderControl(writer);
                    }
                    else if (item.TypeCode == ProductPropertyType.TextField)
                    {
                        var input = new HtmlTextArea();
                        input.ID = "ProductTypeProperty" + item.Id;
                        var defaultValue = item.IsLocalizable ? item.DefaultLocalizableValue : item.DefaultValue;
                        if (propertyValue != null)
                        {
                            input.Value = propertyValue;
                        }
                        else
                        {
                            input.Value = defaultValue;
                        }
                        input.Rows = 5;
                        input.Cols = 40;
                        input.RenderControl(writer);
                    }
                    else if (item.TypeCode == ProductPropertyType.FileUpload)
                    {
                        //TODO:
                    }

                    writer.Flush();
                    return(sw.ToString());
                }
            }
        }
        protected void GenerateProductTypePropertyFields()
        {
            ProductTypePropertiesLiteral.Text = "";
            if (lstProductType.SelectedValue.Trim() != string.Empty)
            {
                string productTypeBvin       = lstProductType.SelectedValue;
                List <ProductProperty> props = MTApp.CatalogServices.ProductPropertiesFindForType(productTypeBvin);
                StringBuilder          sb    = new StringBuilder();
                int count = 0;
                foreach (ProductProperty item in props)
                {
                    count += 1;
                    StringWriter   sw     = new StringWriter();
                    HtmlTextWriter writer = new HtmlTextWriter(sw);
                    sb.Append("<tr><td class=\"formlabel\">");
                    sb.Append(item.DisplayName);
                    sb.Append("</td><td class=\"formfield\">");
                    if (item.TypeCode == ProductPropertyType.CurrencyField)
                    {
                        HtmlInputText input = new HtmlInputText();
                        input.ID = "ProductTypeProperty" + count.ToString();
                        if (ProductTypeProperties.Count > (count - 1))
                        {
                            if (ProductTypeProperties[count - 1] != null)
                            {
                                input.Value = ProductTypeProperties[count - 1];
                            }
                            else
                            {
                                input.Value = item.DefaultValue;
                            }
                        }
                        else
                        {
                            input.Value = item.DefaultValue;
                        }
                        input.RenderControl(writer);
                        writer.Flush();
                        sb.Append(sw.ToString());
                    }
                    else if (item.TypeCode == ProductPropertyType.DateField)
                    {
                        HtmlInputText input = new HtmlInputText();
                        input.ID = "ProductTypeProperty" + count.ToString();
                        if (ProductTypeProperties.Count > (count - 1))
                        {
                            if (ProductTypeProperties[count - 1] != null)
                            {
                                input.Value = ProductTypeProperties[count - 1];
                            }
                            else
                            {
                                input.Value = item.DefaultValue;
                            }
                        }
                        else
                        {
                            input.Value = item.DefaultValue;
                        }
                        input.RenderControl(writer);
                        writer.Flush();
                        sb.Append(sw.ToString());
                    }
                    else if (item.TypeCode == ProductPropertyType.HyperLink)
                    {
                        HtmlInputText input = new HtmlInputText();
                        input.ID = "ProductTypeProperty" + count.ToString();
                        if (ProductTypeProperties.Count > (count - 1))
                        {
                            if (ProductTypeProperties[count - 1] != null)
                            {
                                input.Value = ProductTypeProperties[count - 1];
                            }
                            else
                            {
                                input.Value = item.DefaultValue;
                            }
                        }
                        else
                        {
                            input.Value = item.DefaultValue;
                        }
                        input.RenderControl(writer);
                        writer.Flush();
                        sb.Append(sw.ToString());
                    }
                    else if (item.TypeCode == ProductPropertyType.MultipleChoiceField)
                    {
                        HtmlSelect input = new HtmlSelect();
                        input.ID = "ProductTypeProperty" + count.ToString();
                        bool setWidth = false;
                        foreach (ProductPropertyChoice choice in item.Choices)
                        {
                            if (choice.ChoiceName.Length > 25)
                            {
                                setWidth = true;
                            }
                            System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem(choice.ChoiceName, choice.Id.ToString());
                            input.Items.Add(li);
                        }
                        if (setWidth)
                        {
                            input.Style.Add("width", "305px");
                        }

                        if (ProductTypeProperties.Count > (count - 1))
                        {
                            if (ProductTypeProperties[count - 1] != null)
                            {
                                input.Value = ProductTypeProperties[count - 1];
                            }
                            else
                            {
                                input.Value = item.DefaultValue;
                            }
                        }
                        else
                        {
                            input.Value = item.DefaultValue;
                        }
                        input.RenderControl(writer);
                        writer.Flush();
                        sb.Append(sw.ToString());
                    }
                    else if (item.TypeCode == ProductPropertyType.TextField)
                    {
                        HtmlTextArea input = new HtmlTextArea();
                        input.ID = "ProductTypeProperty" + count.ToString();
                        if (ProductTypeProperties.Count > (count - 1))
                        {
                            if (ProductTypeProperties[count - 1] != null)
                            {
                                input.Value = ProductTypeProperties[count - 1];
                            }
                            else
                            {
                                input.Value = item.DefaultValue;
                            }
                        }
                        else
                        {
                            input.Value = item.DefaultValue;
                        }
                        input.Rows = 5;
                        input.Cols = 40;
                        input.RenderControl(writer);
                        writer.Flush();
                        sb.Append(sw.ToString());
                    }
                    sb.Append("</td></tr>");
                    ProductTypePropertiesLiteral.Text = sb.ToString();
                }
            }
        }