渲染时如何从 WebControl 中删除 span 标签
当使用 ASP.NET CheckBox
(在特殊情况下,继承自 CheckBox
)时,它会在复选框输入控件周围呈现一个跨度,此跨度控件会影响 jQuery 脚本。
渲染时可以去掉这个跨度吗?
When using an ASP.NET CheckBox
(and in out case, inherited from a CheckBox
) it renders a span around the checkbox input control, this span control is affecting jQuery scripts.
Is it possible to remove this span when rendering?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
发现这个有用的提示:
在代码隐藏中,使用 InputAttributes 而不是 Attributes。
例如,键入此:
而不是此:
或而不是内联声明:
最后两个将导致换行跨度。
Found this useful tip:
In Code Behind, use InputAttributes instead of Attributes.
For Example, type this:
instead of this:
or instead of inline declarations:
The last two will cause the wrapping span.
我刚刚在测试页上尝试过这个,但我没有得到我的 CheckBox 控件...您确定是 CheckBox 正在渲染它吗?是有条件的吗?
更新:好的,这似乎取决于 CheckBox 是否具有额外的属性,包括 CssClass 设置...或“禁用”属性。
I just tried this on a test page and I'm not getting the around my CheckBox controls... are you sure it's the CheckBox that's rendering this? Is it conditional?
UPDATE: OK, it appears to be conditional on whether or not the CheckBox has extra attributes, including a CssClass setting... or a "disabled" attribute.
我不知道这是否适用于这个特定的例子。但是,如果您制作自己的 WebControl 并且您永远不希望它在自身周围呈现跨度,您可以像这样重写 Controls 渲染方法:
我猜您可以将相同的内容添加到继承的 CheckBox...类似这样的内容
:解决方案在这里有更好的解释:
http://www.marten-online.com/net /stripping-span-tag-from-webcontrol.html
I don't know if this will work on this particular example. But if you make your own WebControl and you never want it to render spans around itself you can override the Controls render method like this:
I guess that you could add the same to an inherited CheckBox... something like this:
The basic problem with the solution is better explained here:
http://www.marten-online.com/net/stripping-span-tag-from-webcontrol.html
我花了过去 3 个小时的时间来解决这个问题。
结果如下:
除了自定义控件之外,您还需要一个自定义 HtmlTextWriter:
仅供参考,使用:
具有相同的效果,但是:
I spent the last 3 hours pulling my hair to find a solution at this problem.
Here is what came out:
Along with the custom control, you'll need a custom HtmlTextWriter:
Just FYI, using:
has the same effect but:
使用 Jquery
Using Jquery
应该做
should do
如果您不需要标签,可以直接使用输入/复选框控件,或者可以自己放置一个:
CSS 适配器可能能够删除复选框/标签周围的跨度,但我还没有看到用于此目的的跨度。
You can use the input/checkbox control directly if you don't need a label, or can put one yourself:
A CSS Adapter may be able to remove the span around the checkbox/label, but I haven't seen one for that purpose.
为什么不使用 .remove 和 jquery 删除跨度?
Why don't you remove the span using .remove with jquery ?
您可以使用文字控件来代替吗?这两种选择之间有很大的区别:
Can you use a literal control instead? There's a big difference between these two alternatives:
我发现通过实现如下所示的构造函数,您可以为控件指定容器标记。
您可以将
HtmlTextWriterTag
替换为任何可用选项,例如上例中的 Div。默认值为Span
。I've found that by implementing a constructor like the one below, you can specify the container tag for your control.
You can replace the
HtmlTextWriterTag
with any of the aviable options, such as Div in the example above. The default isSpan
.尝试向您的类添加一个如下所示的构造函数:
如果您注意到,控件将默认呈现为跨度,因为这就是“WebControl”使用的内容(如果您使用 Reflector):
参考:
http://aspnetresources.com/blog/stripping_span_from_webcontrol
Try adding a constructor to your class that looks like this:
If you notice, controls will render as spans as default since that's what the "WebControl" uses (if you use Reflector) :
Reference:
http://aspnetresources.com/blog/stripping_span_from_webcontrol
我想知道,没有人提到这一点有什么原因吗:
我读到了这个http://aspnetresources.com/blog/stripping_span_from_webcontrol< /a> flaviotsf 提到的
i wonder, is there a reason no1 mentioned this:
i got this reading http://aspnetresources.com/blog/stripping_span_from_webcontrol that flaviotsf mentioned
这将允许您正常使用该复选框,仍然让它调用服务器端代码,并使用引导程序中的切换。 (我使用的是 Inspina 主题,但其他切换的格式应该相同)
This will allow you to use the check box normally, still have it call server side code, and use the toggle from bootstrap. (I'm using the Inspina theme but it should be the same format for other toggles)
我刚刚遇到这个问题并使用了乔恩的答案,这很好而且有效。缺点是您的类是在代码隐藏中定义的,而不是在标记中定义的。
因此,我接受了答案,并采用了一种编程方式来检索控件的所有属性,将它们复制到 InputAttributes 并从属性中删除这些复制的属性。
请注意,虽然它扩展自 RadioButton,但您可以使用该方法扩展任何控件,例如标签或复选框。
这样,您只需执行以下操作,它将输出不带跨度的标签。
HTML 输出:
(注意“生成”是自动生成的)
I just had this issue and used Jon's answer, which is good and it works. The downside is that your class is defined within the codebehind and not your markup.
So I took the answer and made a progamatic way to retrieve all attributes for the control, copy them to InputAttributes and remove those copied attributes from attributes.
Note that while this extends from RadioButton, you could use the method to extend any control, such as labels or checkboxes.
This way, all you need to do is the following, and it will output tags without the span.
HTML output:
(note that "generated" is autogenerated)