本文整理汇总了C#中TagBuilder.GenerateId方法的典型用法代码示例。如果您正苦于以下问题:C# TagBuilder.GenerateId方法的具体用法?C# TagBuilder.GenerateId怎么用?C# TagBuilder.GenerateId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TagBuilder
的用法示例。
在下文中一共展示了TagBuilder.GenerateId方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Foo
public static HtmlString Foo(this HtmlHelper h, string attr, string id, string tagElement)
{
var tBuilder = new TagBuilder(tagElement);
tBuilder.GenerateId(attr + id);
var c = new HtmlString(tBuilder.ToString());
return c;
}
示例2: HtmlTag
public HtmlTag(string tagName, string id)
{
tagBuilder = new TagBuilder(tagName);
if (!string.IsNullOrEmpty(id))
tagBuilder.GenerateId(id);
}
示例3: Catalogue
public static HtmlString Catalogue(this HtmlHelper helper, string id)
{
// Create tag builder
var builder = new TagBuilder("ul");
StringBuilder innerHtml = new StringBuilder();
// Create valid id
builder.GenerateId(id);
// Render tag
if (HttpContext.Current.Session["Cart"] != null) // haven't been to db yet
{
CartItem[] cart = (CartItem[])HttpContext.Current.Session["Cart"];
foreach (CartItem item in cart)
{
innerHtml.Append("<li>");
innerHtml.Append("<div class='img'>");
innerHtml.Append("<img alt='' src='Content/images/");
innerHtml.Append(item.Graphic + "' id='Graphic" + item.ProdCd + "' /></div>");
innerHtml.Append("<div class='info'><div style='font-size:small;background:#cfcfcf;' id='Name");
innerHtml.Append(item.ProdCd + "'>" + item.ProdName + "</div>");
innerHtml.Append("<p id='Descr" + item.ProdCd + "' data-description='");
innerHtml.Append(item.Description + "'>" + item.Description.Substring(0, 20) + "...</p>");
innerHtml.Append("<div class='price'><span class='st'>Our price:</span>");
innerHtml.Append("<strong id='Price" + item.ProdCd + "'>");
innerHtml.Append(String.Format("{0:C}", Convert.ToDecimal(item.Msrp)));
innerHtml.Append("</strong></div><div class='actions'>");
innerHtml.Append("<a href=\"#details_popup\" onclick=\"copyProdInfoToPopup('");
innerHtml.Append(item.ProdCd + "');\">Details</a></div></div></li>");
}
}
builder.InnerHtml = innerHtml.ToString();
return new HtmlString(builder.ToString());
}
示例4: Dropdown
/// <summary>
/// Render simple 'select' control. <c>IsSelected</c> property
/// of items will be used to select option.
/// </summary>
/// <param name="items">Items list</param>
/// <returns>Formatted HTML</returns>
public static IHtmlString Dropdown(string fieldName, IEnumerable<SelectListItem> items)
{
Throw.IfNullArgument(items, "items");
var tb = new TagBuilder("select");
tb.MergeAttribute("name", fieldName);
tb.GenerateId(fieldName);
var sb = new StringBuilder();
foreach (var item in items)
{
var opt = new TagBuilder("option");
if(!String.IsNullOrEmpty(item.Value))
opt.MergeAttribute("value", item.Value);
if (item.Selected)
opt.MergeAttribute("selected", "selected");
opt.InnerHtml = item.Text;
sb.Append(opt.ToString(TagRenderMode.Normal));
}
tb.InnerHtml = sb.ToString();
return MvcHtmlString.Create(tb.ToString());
}
示例5: DropList
public static string DropList(this HtmlHelper helper, string name, List<SelectListItem> data, object htmlAttributes = null)
{
if (data == null && helper.ViewData != null)
data = helper.ViewData.Eval(name) as List<SelectListItem>;
if (data == null) return string.Empty;
var select = new TagBuilder("select");
if (htmlAttributes != null)
select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
select.GenerateId(name);
select.MergeAttribute("name" , name);
var optHtml = new StringBuilder();
foreach (var item in data)
{
var option = new TagBuilder("option");
option.Attributes.Add("value", helper.Encode(item.Value));
if (item.Selected)
option.Attributes.Add("selected", "selected");
option.InnerHtml = helper.Encode(item.Text);
optHtml.AppendLine(option.ToString(TagRenderMode.Normal));
}
select.InnerHtml = optHtml.ToString();
return select.ToString(TagRenderMode.Normal);
}
示例6: BuildRadioButton
private IHtmlString BuildRadioButton(string name, object value, bool? isChecked, IDictionary<string, object> attributes) {
string valueString = ConvertTo(value, typeof(string)) as string;
TagBuilder builder = new TagBuilder("input");
builder.MergeAttribute("type", "radio", true);
builder.GenerateId(name);
builder.MergeAttributes(attributes, replaceExisting: true);
builder.MergeAttribute("value", valueString, replaceExisting: true);
builder.MergeAttribute("name", name, replaceExisting: true);
var modelState = ModelState[name];
string modelValue = null;
if (modelState != null) {
modelValue = ConvertTo(modelState.Value, typeof(string)) as string;
isChecked = isChecked ?? String.Equals(modelValue, valueString, StringComparison.OrdinalIgnoreCase);
}
if (isChecked.HasValue) {
// Overrides attribute values
if (isChecked.Value) {
builder.MergeAttribute("checked", "checked", true);
}
else {
builder.Attributes.Remove("checked");
}
}
AddErrorClass(builder, name);
return builder.ToHtmlString(TagRenderMode.SelfClosing);
}
示例7: Process
public override void Process(TagHelperContext context, TagHelperOutput output)
{
TagBuilder table = new TagBuilder("table");
table.GenerateId(context.UniqueId, "id");
var attributes = context.AllAttributes.Where(a => a.Name != ItemsAttributeName).ToDictionary(a => a.Name);
table.MergeAttributes(attributes);
var tr = new TagBuilder("tr");
var heading = Items.First();
PropertyInfo[] properties = heading.GetType().GetProperties();
foreach (var prop in properties)
{
var th = new TagBuilder("th");
th.InnerHtml.Append(prop.Name);
tr.InnerHtml.AppendHtml(th);
}
table.InnerHtml.AppendHtml(tr);
foreach (var item in Items)
{
tr = new TagBuilder("tr");
foreach (var prop in properties)
{
var td = new TagBuilder("td");
td.InnerHtml.Append(prop.GetValue(item).ToString());
tr.InnerHtml.AppendHtml(td);
}
table.InnerHtml.AppendHtml(tr);
}
output.Content.AppendHtml(table.InnerHtml);
}
示例8: Topic
public static string Topic(string name, List<Topic> options)
{
var select = new TagBuilder("select");
select.MergeAttribute("style", "background-color: transparent;");
select.MergeAttribute("name", name);
select.GenerateId(name);
var optionBuilder = new StringBuilder();
foreach (var option in options)
{
var optionTag = new TagBuilder("option");
optionTag.MergeAttribute("value", option.Id.ToString());
optionTag.MergeAttribute("style",
"color: white; background-color: " +
ColorTranslator.ToHtml(option.Color));
optionTag.InnerHtml = option.Name;
optionBuilder.Append(optionTag.ToString(TagRenderMode.Normal));
}
select.InnerHtml = optionBuilder.ToString();
return select.ToString(TagRenderMode.Normal);
}
示例9: GroupBox
public GroupBox(AjaxHelper ajax, string headerText, string loader)
{
_ajax = ajax;
if (IsAjax)
return;
_groupDiv = new TagBuilder("div");
var headerDiv = new TagBuilder("div");
_contentDiv = new TagBuilder("div");
_loadingDiv = new TagBuilder("div");
_groupDiv.AddCssClass("card");
headerDiv.AddCssClass("card-header");
_contentDiv.AddCssClass("card-block");
_contentDiv.Attributes.Add("style", "position:relative;");
_loadingDiv.AddCssClass("ajax-overlay");
_loadingDiv.GenerateId(loader);
headerDiv.SetInnerText(headerText);
Write(_groupDiv.ToString(TagRenderMode.StartTag));
Write(headerDiv.ToString());
Write(_contentDiv.ToString(TagRenderMode.StartTag));
}
示例10: TextAreaHelper
private static string TextAreaHelper(this HtmlHelper htmlHelper, string name, bool useViewData, string value, IDictionary<string, object> implicitAttributes, IDictionary<string, object> explicitParameters, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(name)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
}
TagBuilder tagBuilder = new TagBuilder("textarea");
// Add implicit attributes.
tagBuilder.MergeAttributes(implicitAttributes);
tagBuilder.GenerateId(name);
// Merge htmlAttributes.
tagBuilder.MergeAttributes(htmlAttributes, true);
// Override all the attributes with explicit parameters.
tagBuilder.MergeAttributes(explicitParameters, true);
tagBuilder.MergeAttribute("name", name, true);
// If there are any errors for a named field, we add the css attribute.
ModelState modelState;
if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState)) {
if (modelState.Errors.Count > 0) {
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
}
// The first newline is always trimmed when a TextArea is rendered, so we add an extra one
// in case the value being rendered is something like "\r\nHello".
// The attempted value receives precedence over the explicitly supplied value parameter.
string attemptedValue = (string)htmlHelper.GetModelStateValue(name, typeof(string));
tagBuilder.SetInnerText(Environment.NewLine + (attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : value)));
return tagBuilder.ToString(TagRenderMode.Normal);
}
示例11: ToHtmlString
public override string ToHtmlString()
{
string innerText = Text ?? string.Empty;
string fullName = Name;
string tagName = TagName.HasValue() ? TagName.ToLower() : "span";
var tagBuilder = new TagBuilder(tagName);
if (Encode)
tagBuilder.SetInnerText(innerText);
else
tagBuilder.InnerHtml = innerText;
tagBuilder.MergeAttributes(HtmlAttributes);
if (fullName.HasValue())
tagBuilder.GenerateId(fullName);
foreach (string cssClass in CssClasses)
tagBuilder.AddCssClass(cssClass);
if (CanWrite())
{
if (GrantedClass.HasValue())
tagBuilder.AddCssClass(GrantedClass);
}
else
{
if (DeniedClass.HasValue())
tagBuilder.AddCssClass(DeniedClass);
}
return tagBuilder.ToString(TagRenderMode.Normal);
}
示例12: TagBuilder
public virtual TagBuilder 生成标签构造器()
{
if (是否已进行过生成) throw new Exception("此对象无法执行多次生成代码操作。");
var tb = new TagBuilder(标签名称);
if (!Id.IsNullOrEmpty()) tb.GenerateId(Id);
if (Css类列表 != null)
{
foreach (var f in Css类列表)
{
tb.AddCssClass(f);
}
}
if (属性字典 != null)
{
foreach (var f in 属性字典)
{
tb.Attributes.Add(f.Key, f.Value);
}
}
if (子元素列表 != null)
{
foreach (var f in 子元素列表)
{
tb.InnerHtml += f.生成代码();
}
}
是否已进行过生成 = true;
return tb;
}
示例13: BuildCheckBox
private IHtmlString BuildCheckBox(string name, bool? isChecked, IDictionary<string, object> attributes)
{
TagBuilder builder = new TagBuilder("input");
builder.MergeAttribute("type", "checkbox", replaceExisting: true);
builder.GenerateId(name);
builder.MergeAttributes(attributes, replaceExisting: true);
builder.MergeAttribute("name", name, replaceExisting: true);
if (UnobtrusiveJavaScriptEnabled)
{
var validationAttributes = _validationHelper.GetUnobtrusiveValidationAttributes(name);
builder.MergeAttributes(validationAttributes, replaceExisting: false);
}
var model = ModelState[name];
if (model != null && model.Value != null)
{
bool modelValue = (bool)ConvertTo(model.Value, typeof(bool));
isChecked = isChecked ?? modelValue;
}
if (isChecked.HasValue)
{
if (isChecked.Value == true)
{
builder.MergeAttribute("checked", "checked", replaceExisting: true);
}
else
{
builder.Attributes.Remove("checked");
}
}
AddErrorClass(builder, name);
return builder.ToHtmlString(TagRenderMode.SelfClosing);
}
示例14: Upload
internal static string Upload(this HtmlHelper htmlHelper, string name, object value, IDictionary<string, object> htmlAttributes)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("Value cannot be null or empty.", "name");
var input = new TagBuilder("input");
input.GenerateId(name + "Upload");
input.MergeAttribute("name", name + "Upload");
input.MergeAttribute("type", "file");
input.MergeAttributes(htmlAttributes);
string output = string.Format("{0}{1}", htmlHelper.Hidden(name, value), input);
if (value is string && !string.IsNullOrEmpty(value as string))
{
var delete = new TagBuilder("input");
delete.GenerateId(name + "SqlDelete");
delete.MergeAttribute("name", name + "SqlDelete");
delete.MergeAttribute("type", "checkbox");
delete.MergeAttribute("value", "true");
output = string.Format("{0} <label>{1} SqlDelete</label>", output, delete);
}
return output;
}
示例15: Image
public static string Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)
{
// Instantiate a UrlHelper
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
// Create tag builder
var builder = new TagBuilder("img");
if (!string.IsNullOrEmpty(id))
{
builder.GenerateId(id);
}
// Add attributes
builder.MergeAttribute("src", urlHelper.Content(url));
if (!string.IsNullOrEmpty(alternateText))
{
builder.MergeAttribute("alt", alternateText);
builder.MergeAttribute("title", alternateText);
}
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
// Render tag
return builder.ToString(TagRenderMode.SelfClosing);
}