Ejemplo n.º 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());
        }
        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();
                }
            }
        }