Asp.net复选框和HTML数据属性 [英] Asp.net checkbox and html data attribute

查看:112
本文介绍了Asp.net复选框和HTML数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net,如果使用自定义属性,通常它被渲染原样。

In asp.net, if you use a custom attribute, usually it is rendered as-is.

考虑到这个标记(注意:属性,如 ID 名称在所有的例子作为自己生成的ID被删除/名称详细):

Considering this markup (note: attributes such as id, name and for were removed in all examples as their generated id/names are verbose):

<asp:TextBox runat="server" data-foo="bar" />

呈现在asp.net为:

Is rendered in asp.net as:

<input type="text" data-foo="bar" />

也就是说,asp.net保持数据富不变。

复选框通常呈现的是这样的:

Check box are usually rendered like this:

<asp:CheckBox runat="server" Text="Normal" />

将呈现为:

<input type="checkbox" />
<label>Normal</label>

但是,如果你在一个复选框添加自定义属性:

But if you add a custom attribute on a checkbox:

<asp:CheckBox runat="server" Text="Custom attribute" data-foo="bar"  />

它呈现为:

<span data-foo="bar">
    <input type="checkbox" />
    <label>Custom attribute</label>
</span>

正如你所看到的,呈现一个跨度来保存属性。如果您在code添加的属性后面也会发生这种情况。这不与任何其他HTMLControl时发生的,据我所知。

As you can see, a span in rendered to hold the attribute. This also happens if you add the attribute in code behind. This does not happen with any other HtmlControl, AFAIK.

有谁知道为什么这个跨度呈现举行属性?

Does anyone know why this span is rendered to hold the attribute?

反正是有呈现输入标签属性?

Is there anyway to render the attribute in the input tag?

推荐答案

我不知道为什么它有一个跨度呈现,但我想你可以直接添加属性到输入复选框 在code-behid 这样的元素:

I'm not sure why it's rendered with a span, but I suppose you can add the attribute directly to the input element of the CheckBox in code-behid like this:

myCheckBox.InputAttributes.Add(...)

参考链接:

更新

这是额外的父元素被使用,让您应用到属性的复选框可以同时影响输入和文本。我想这是一个跨度(而不是 DIV ),因为它是一个内联元素 ,使得在不同的场景使用更方便。

An additional parent element is used, so that the attributes you apply to a CheckBox can affect both the input and the text. I suppose it's a span (and not a div), because it's an inline element, making it more convenient to use in different scenarios.

参考链接:

  • http://en.wikipedia.org/wiki/Span_and_div#Differences_and_default_behavior
  • http://learnwebdesignonline.com/span-div

这篇关于Asp.net复选框和HTML数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆