English 中文(简体)
Asp.net 复选框和 html 数据属性
原标题:Asp.net checkbox and html data attribute

在 asp.net 中,如果使用自定义属性,通常会按原样设定。

考虑到此标记( < 强度 > 注: < 强度 > 属性, 如 < code> id 、 < code> name 和 < code> for , 在所有例子中都删除了, 因为它们生成的 ID/ names 是动词 ):

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

以 asp.net 制成的 :

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

也就是说, asp.net 保持 < code> data- foo 不被触及 。

复选框通常以这样的方式设定 :

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

重命名为 :

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

但如果在复选框中添加自定义属性 :

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

其规定如下:

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

如您所看到的, 用于保持属性的间隔。 如果您在后面的代码中添加属性, 也会发生这种情况。 这不会发生在其他 Html Control, AFAIK 中 。

有谁知道为什么这个宽度是用来维持属性的吗?

输入标记中是否要设定属性?

最佳回答

我不知道为什么它是用一个横幅来制作的, 但我想您可以直接将属性添加到 input 元素上, 即 checkBox 的 < strong > in code- behid 中 :

myCheckBox.InputAttributes.Add(...)

参考链接 :

<强 > 更新

使用另一个父元素,这样您对 checkBox 应用的属性既可以影响 input 和文本。我想它是 span (而不是 div ),因为它是 < strong > insline 元素 ,因此更便于在不同情况下使用。

参考链接 :

问题回答

使引擎以这种方式构建 checkBox 控制, 没有什么可做的。

您可以做的就是创建一个 runat="server" 输入。

<input id="myInput" runat="server" type="checkbox" data-foo="bar"/>
<label>Custom attribute</label>

另一个选项是添加 data-foo 属性, 使用文档加载时的jquery

$(function(){
    $( span[data-foo] ).each(function(){
        var $span = $(this);
        var value = $span.data( foo );
        $span.find( input ).data( foo ,value);        
    });
})

仅添加另一种方法, 当所有其他方法都失败时, 我使用的方法, 您总是可以使用字面控制, 并让它成为任何你想要的。 您在处理后退时需要多做点工作, 但有时这是获取您需要的 html 的唯一方法 。

标记 :

<asp:Literal ID="myLiteral" runat="server"/>

代号 :

myLiteral.Text = string.Format("<input type="checkbox" data-foo="{0}" /><label>Normal</label>", value)

如果您试图在单击事件上访问该属性,您可以通过选择控件来做到这一点。例如,我有一个复选框,我像您一样指定一个自定义属性

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

现在在 on Checked Changed 上,我需要获得这个值,用我的方法,我得到了一个 sender 对象。

protected void chk1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox myControl = (CheckBox)sender;
    string value = myControl.Attributes["data-foo"].ToString();

}

我希望这能帮到某人





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签