/// <summary> /// Renders the ScriptManager to the page. /// </summary> public void Render() { TextWriter writer = _context.Context.Response.Output; HtmlElementBuilder builder = new HtmlElementBuilder("script", new { Type = "text/javascript" }, String.Empty); foreach (string path in _includes.Values) { builder.Attributes.Merge("src", path, true); writer.WriteLine(builder.ToString()); } if (_blocks.Values.Any(b => !b.IsEmpty)) { builder.Attributes.Remove("src"); writer.WriteLine(builder.ToString(HtmlElementRenderMode.StartTag)); writer.WriteLine("//<![CDATA["); foreach (ScriptBlock script in _blocks.Values) { script.Render(writer); } writer.WriteLine("//]]>"); writer.WriteLine(builder.ToString(HtmlElementRenderMode.EndTag)); } _blocks.Clear(); _includes.Clear(); }
private static string InputBuilder(HtmlControlHelper helper, string type, string name, object value, IDictionary <string, object> attributes) { if (attributes == null) { attributes = new ValueDictionary(); } if (value == null) { attributes.Remove("value"); } HtmlElementBuilder htb = new HtmlElementBuilder("input"); htb.Attributes.Merge(attributes, true); htb.Attributes.Merge("name", name, true); htb.Attributes.Merge("type", type, true); if (value != null) { htb.Attributes.Merge("value", value, true); } return(htb.ToString()); }
public string Tag(string tagName, object attributes, params string[] content) { HtmlElementBuilder builder = new HtmlElementBuilder(tagName, attributes, (content == null) ? null : String.Join("", content)); return(builder.ToString()); }
protected virtual void Dispose(bool disposing) { if (disposing && !_disposed) { _disposed = true; HtmlElementBuilder builder = new HtmlElementBuilder("form"); _response.Write(builder.ToString(HtmlElementRenderMode.EndTag)); } }
/// <summary> /// Returns the string for a hidden input containing a /// token used to prevent CSRF attacks. /// </summary> /// <param name="value">An extended value to store in the token.</param> public string Token(string value) { RequestValidationToken token = RequestValidationToken.Create(Context.Context); HtmlElementBuilder builder = new HtmlElementBuilder("input"); builder.Attributes.Add("type", "hidden"); builder.Attributes.Add("name", ValidateRequestTokenAttribute.ValidationFieldName); builder.Attributes.Add("value", token.Serialize()); return(builder.ToString()); }
private static string LinkBuilder(HtmlControlHelper helper, string url, string text, IDictionary <string, object> attributes) { HtmlElementBuilder builder = new HtmlElementBuilder("a"); if (attributes == null) { attributes = new ValueDictionary(); } builder.Attributes.Merge(attributes); builder.Attributes.Merge("href", url, true); builder.InnerHtml = text; return(builder.ToString()); }
public static string Serialize(this HtmlHelper helper, string name, object data, SerializationMode mode) { Precondition.Require(helper, () => Error.ArgumentNull("helper")); Precondition.Defined(name, () => Error.ArgumentNull("name")); ModelStateSerializer serializer = new ModelStateSerializer(); string elementName = helper.Context.ViewData.Template.GetHtmlElementName(name); string value = serializer.Serialize(data ?? helper.Context.ViewData.Model, mode); HtmlElementBuilder builder = new HtmlElementBuilder("input"); builder.Attributes["type"] = "hidden"; builder.Attributes["name"] = elementName; builder.Attributes["value"] = value; return(builder.ToString()); }
private static MvcForm FormBuilder(HtmlControlHelper helper, FormMethod method, string action, IDictionary <string, object> attributes) { HtmlElementBuilder builder = new HtmlElementBuilder("form"); HttpResponseBase response = helper.Context.Context.Response; if (attributes == null) { attributes = new ValueDictionary(); } builder.Attributes.Merge(attributes); builder.Attributes.Merge("action", action, true); builder.Attributes.Merge("method", GetFormMethod(method), true); response.Write(builder.ToString(HtmlElementRenderMode.StartTag)); return(new MvcForm(response)); }
private static string TextAreaBuilder(HtmlControlHelper helper, string name, string value, int rows, int columns, IDictionary <string, object> attributes) { if (attributes == null) { attributes = new ValueDictionary(); } HtmlElementBuilder htb = new HtmlElementBuilder("textarea"); htb.Attributes.Merge(attributes, true); htb.Attributes.Merge("name", name, true); htb.Attributes.Merge("rows", rows, false); htb.Attributes.Merge("cols", columns, false); htb.InnerText = value ?? String.Empty; return(htb.ToString()); }
private static string SelectBuilder(HtmlControlHelper helper, string name, MultiSelectList dataSource, bool isMultiple, IDictionary <string, object> attributes) { Precondition.Defined(name, () => Error.ArgumentNull("name")); if (attributes == null) { attributes = new ValueDictionary(); } if (isMultiple) { attributes["multiple"] = "multiple"; } else { attributes.Remove("multiple"); } HtmlElementBuilder outer = new HtmlElementBuilder("select"); outer.Attributes.Merge(attributes, true); outer.Attributes.Merge("name", name, true); if (dataSource != null) { StringBuilder builder = new StringBuilder(); IEnumerable <ListItem> listItems = dataSource.GetListItems(); string[] selectedValues = null; if (helper.DataSource.Keys.Any(k => k.Equals(name, StringComparison.OrdinalIgnoreCase))) { selectedValues = helper.DataSource.GetValue <string>(name, String.Empty) .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); } builder.AppendLine(); foreach (ListItem item in listItems) { if (selectedValues != null) { item.Selected = selectedValues.Any(s => String.Equals(item.Value, s, StringComparison.OrdinalIgnoreCase)); } HtmlElementBuilder inner = new HtmlElementBuilder("option"); inner.InnerText = item.Text; if (item.Value != null) { inner.Attributes.Merge("value", item.Value, true); } if (item.Selected) { inner.Attributes.Merge("selected", "selected"); } builder.AppendLine(inner.ToString()); } outer.InnerHtml = builder.ToString(); } return(outer.ToString()); }
public string Tag(string tagName, object attributes) { HtmlElementBuilder builder = new HtmlElementBuilder(tagName, attributes, null); return(builder.ToString()); }