protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) { if (this.Items.Count > 0) { bool selected = false; bool optGroupStarted = false; for (int i = 0; i < this.Items.Count; i++) { ListItem item = this.Items[i]; if (item.Enabled) { if (item.Value == "$$OPTGROUP$$OPTGROUP$$") { if (optGroupStarted) { writer.WriteEndTag("optgroup"); } writer.WriteBeginTag("optgroup"); writer.WriteAttribute("label", item.Text); writer.Write('>'); writer.WriteLine(); optGroupStarted = true; } else { writer.WriteBeginTag("option"); if (item.Selected) { if (selected) { this.VerifyMultiSelect(); } selected = true; writer.WriteAttribute("selected", "selected"); } writer.WriteAttribute("value", item.Value, true); if (item.Attributes.Count > 0) { item.Attributes.Render(writer); } if (this.Page != null) { this.Page.ClientScript.RegisterForEventValidation( this.UniqueID, item.Value); } writer.Write('>'); HttpUtility.HtmlEncode(item.Text, writer); writer.WriteEndTag("option"); writer.WriteLine(); } } } if (optGroupStarted) { writer.WriteEndTag("optgroup"); } } }
/// <summary> /// Writes the Html data to the output stream. /// </summary> /// <param name="writer">The Html text writer to the current Mvc view context output stream.</param> protected override void WriteHtml(System.Web.UI.HtmlTextWriter writer) { writer.WriteBeginTag("ul"); writer.WriteAttribute("id", Id); writer.WriteAttribute("class", "filetree"); writer.Write(">\r\n"); writer.WriteEndTag("ul"); }
protected override void Render(System.Web.UI.HtmlTextWriter writer) { base.Render(writer); writer.Write("<SELECT name=\"" + this.UniqueID + "\" class=\"" + this.CssClass + "\">"); for (int i = 0; i < _values.Count; i++) { string val = (string)_values[i]; if (_groups.Contains(val)) { writer.Write("<OPTGROUP "); writer.WriteAttribute("label", val); writer.Write(" />"); } else { string capt = (string)_captions[i]; writer.Write("<OPTION"); if (capt != val) { writer.Write(" VALUE=\"" + val.Replace("\"", "\"\"") + "\">"); } else { writer.Write(">"); } writer.Write(capt); // writer.Write("</OPTION>"); } } writer.Write("</SELECT>"); }
protected override void BuildAttribute(System.Web.UI.HtmlTextWriter writer, string param, string content) { param = (param + "").Trim(); if (param == "") { return; } if (regTdAttr.IsMatch(param)) { string[] attr = param.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (attr.Length == 1) { writer.WriteAttribute("width", attr[0]); } if (attr.Length == 3) { writer.WriteAttribute("colspan", attr[0]); writer.WriteAttribute("rowspan", attr[1]); writer.WriteAttribute("width", attr[2]); } } }
/// <summary> /// Generates the select. /// </summary> /// <param name="target">The target.</param> /// <param name="selectedValue">The selected value.</param> /// <param name="dataSource">The data source.</param> /// <param name="attributes">The attributes.</param> /// <returns></returns> protected virtual string GenerateSelect(string target, object selectedValue, IEnumerable dataSource, IDictionary attributes) { var id = CreateHtmlId(target); ApplyValidation(InputElementType.Select, target, ref attributes); var sb = new StringBuilder(); var sbWriter = new StringWriter(sb); var writer = new HtmlTextWriter(sbWriter); string firstOption = null; string firstOptionValue = null; var pascalCaseToWord = false; var name = target; if (attributes != null) { firstOption = CommonUtils.ObtainEntryAndRemove(attributes, "firstoption"); firstOptionValue = CommonUtils.ObtainEntryAndRemove(attributes, "firstoptionvalue"); pascalCaseToWord = Convert.ToBoolean(CommonUtils.ObtainEntryAndRemove(attributes, "pascalCaseToWord")); if (attributes.Contains("name")) { name = (String)attributes["name"]; attributes.Remove("name"); } if (attributes.Contains("id")) { id = (String)attributes["id"]; attributes.Remove("id"); } } var state = SetOperation.IterateOnDataSource(selectedValue, dataSource, attributes); writer.WriteBeginTag("select"); writer.WriteAttribute("id", id); writer.WriteAttribute("name", name); writer.Write(" "); writer.Write(GetAttributes(attributes)); writer.Write(HtmlTextWriter.TagRightChar); writer.WriteLine(); if (firstOption != null) { writer.WriteBeginTag("option"); writer.WriteAttribute("value", (firstOptionValue == null) ? "0" : SafeHtmlEncode(firstOptionValue)); writer.Write(HtmlTextWriter.TagRightChar); writer.Write(SafeHtmlEncode(firstOption)); writer.WriteEndTag("option"); writer.WriteLine(); } foreach (SetItem item in state) { writer.WriteBeginTag("option"); if (item.IsSelected) { writer.Write(" selected=\"selected\""); } writer.WriteAttribute("value", SafeHtmlEncode(item.Value)); writer.Write(HtmlTextWriter.TagRightChar); if (pascalCaseToWord) { writer.Write(SafeHtmlEncode(TextHelper.PascalCaseToWord(item.Text))); } else { writer.Write(SafeHtmlEncode(item.Text)); } writer.WriteEndTag("option"); writer.WriteLine(); } writer.WriteEndTag("select"); return sbWriter.ToString(); }
/// <summary> /// Generates a label element. /// </summary> /// <param name="target">The object to get the value from and to be based on to create the element name.</param> /// <param name="label">Legend</param> /// <param name="attributes">Attributes for the FormHelper method and for the html element it generates</param> /// <returns>The generated form element</returns> public virtual string LabelFor(string target, string label, IDictionary attributes) { target = RewriteTargetIfWithinObjectScope(target); var id = CreateHtmlId(attributes, target); var sb = new StringBuilder(); var sbWriter = new StringWriter(sb); var writer = new HtmlTextWriter(sbWriter); writer.WriteBeginTag("label"); writer.WriteAttribute("for", id); var strAttributes = GetAttributes(attributes); if (strAttributes != String.Empty) { writer.Write(HtmlTextWriter.SpaceChar); } writer.Write(strAttributes); writer.Write(HtmlTextWriter.TagRightChar); writer.Write(label); writer.WriteEndTag("label"); return sbWriter.ToString(); }
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) { // The current control being "adaptered" is available within context from the Control property DropDownList dropDownList = (DropDownList)Control; ListItemCollection items = dropDownList.Items; // Retrieve Optgrouping using LinQ var groups = (from p in items.OfType <ListItem>() group p by p.Attributes["Group"] into g select new { Label = g.Key, Items = g.ToList <ListItem>() }); foreach (var group in groups) { if (!String.IsNullOrEmpty(group.Label)) { writer.WriteBeginTag("optgroup"); writer.WriteAttribute("label", group.Label); writer.Write(">"); } int count = group.Items.Count(); if (count > 0) { bool flag = false; for (int i = 0; i < count; i++) { ListItem item = group.Items[i]; writer.WriteBeginTag("option"); if (item.Selected) { if (flag) { throw new HttpException("Multiple selected items not allowed"); } flag = true; writer.WriteAttribute("selected", "selected"); } if (!item.Enabled) { writer.WriteAttribute("disabled", "true"); } writer.WriteAttribute("value", item.Value, true); if (this.Page != null) { this.Page.ClientScript.RegisterForEventValidation(dropDownList.UniqueID, item.Value); } writer.Write('>'); HttpUtility.HtmlEncode(item.Text, writer); writer.WriteEndTag("option"); writer.WriteLine(); } } if (!String.IsNullOrEmpty(group.Label)) { writer.WriteEndTag("optgroup"); } } }
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) { ListItemCollection items = this.Items; int count = items.Count; if (count > 0) { bool flag = false; string prevOptGroup = null; for (int i = 0; i < count; i++) { string tag = OptionTag; string optgroupLabel = null; ListItem item = items[i]; if (item.Enabled) { if (item.Attributes.Count > 0 && item.Attributes[OptionGroupTag] != null) { optgroupLabel = item.Attributes[OptionGroupTag]; if (prevOptGroup != optgroupLabel) { if (prevOptGroup != null) { writer.WriteEndTag(OptionGroupTag); } writer.WriteBeginTag(OptionGroupTag); if (!string.IsNullOrEmpty(optgroupLabel)) { writer.WriteAttribute("label", optgroupLabel); } writer.Write('>'); } item.Attributes.Remove(OptionGroupTag); prevOptGroup = optgroupLabel; } else { if (prevOptGroup != null) { writer.WriteEndTag(OptionGroupTag); } prevOptGroup = null; } writer.WriteBeginTag(tag); if (item.Selected) { if (flag) { this.VerifyMultiSelect(); } flag = true; writer.WriteAttribute("selected", "selected"); } writer.WriteAttribute("value", item.Value, true); if (item.Attributes.Count > 0) { item.Attributes.Render(writer); } if (optgroupLabel != null) { item.Attributes.Add(OptionGroupTag, optgroupLabel); } if (this.Page != null) { this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value); } writer.Write('>'); HttpUtility.HtmlEncode(item.Text, writer); writer.WriteEndTag(tag); writer.WriteLine(); if (i == count - 1) { if (prevOptGroup != null) { writer.WriteEndTag(OptionGroupTag); } } } } } }
protected override void RenderAttributes(System.Web.UI.HtmlTextWriter writer) { writer.WriteAttribute("name", this.Name); writer.WriteAttribute("method", this.Method); Attributes.Render(writer); }