C# 渲染时如何从WebControl中删除span标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2173331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove span tag from WebControl when rendered
提问by Mark Redman
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.
使用 ASP.NET 时CheckBox
(在 out 情况下,从 a 继承CheckBox
)它在复选框输入控件周围呈现一个跨度,这个跨度控件会影响 jQuery 脚本。
Is it possible to remove this span when rendering?
渲染时是否可以删除此跨度?
采纳答案by Jon
Found this useful tip:
发现这个有用的提示:
In Code Behind, use InputAttributes instead of Attributes.
在代码隐藏中,使用 InputAttributes 而不是 Attributes。
For Example, type this:
例如,输入:
chk.InputAttributes.Add("onchange", "updateFields()")
instead of this:
而不是这个:
chk.Attributes.Add("onchange", "updateFields()")
or instead of inline declarations:
或代替内联声明:
<asp:CheckBox ID="chk" runat="server" onchange="updateFields()" />
The last two will cause the wrapping span.
最后两个将导致包装跨度。
回答by Nick Craver
You can use the input/checkbox control directly if you don't need a label, or can put one yourself:
如果不需要标签,可以直接使用 input/checkbox 控件,也可以自己放一个:
<input type="checkbox" id="CheckBox1" runat="server" />
<label for="CheckBox1">My Label</label>
A CSS Adapter may be able to remove the span around the checkbox/label, but I haven't seen one for that purpose.
CSS Adapter 可能能够移除复选框/标签周围的跨度,但我还没有见过用于此目的的。
回答by Hannoun Yassir
回答by Bryan
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?
我只是在测试页面上尝试过这个,但我没有绕过我的 CheckBox 控件……你确定是 CheckBox 正在呈现这个吗?是有条件的吗?
UPDATE: OK, it appears to be conditional on whether or not the CheckBox has extra attributes, including a CssClass setting... or a "disabled" attribute.
更新:好的,这似乎取决于 CheckBox 是否具有额外的属性,包括 CssClass 设置...或“禁用”属性。
回答by Bradbox
Can you use a literal control instead? There's a big difference between these two alternatives:
你可以改用文字控件吗?这两种选择之间存在很大差异:
<p>12345<asp:Label ID="lblMiddle" runat="server" Text="6"></asp:Label>7890</p>
<p>12345<asp:Literal ID="ltlMiddle" runat="server" Text="6"></asp:Literal>7890</p>
回答by mattsson
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:
我不知道这是否适用于这个特定的例子。但是,如果您创建自己的 WebControl 并且您从不希望它在自身周围呈现跨度,则可以像这样覆盖 Controls 呈现方法:
public class MyWebControl : WebControl
{
/* Your code
between here */
protected override void Render(HtmlTextWriter writer)
{
RenderContents (writer);
}
}
I guess that you could add the same to an inherited CheckBox... something like this:
我想您可以将相同的内容添加到继承的 CheckBox 中……像这样:
public class MyCheckBox : CheckBox
{
/* Your code
between here */
protected override void Render(HtmlTextWriter writer)
{
RenderContents (writer);
}
}
The basic problem with the solution is better explained here:
http://www.marten-online.com/net/stripping-span-tag-from-webcontrol.html
解决方案的基本问题在这里得到了更好的解释:http:
//www.marten-online.com/net/stripping-span-tag-from-webcontrol.html
回答by turtlepick
Try adding a constructor to your class that looks like this:
尝试向您的类添加一个构造函数,如下所示:
public MyControl() : base()
{
}
If you notice, controls will render as spans as default since that's what the "WebControl" uses (if you use Reflector) :
如果您注意到,控件将默认呈现为跨度,因为这是“WebControl”使用的(如果您使用 Reflector):
protected WebControl() : this(HtmlTextWriterTag.Span) { }
Reference: http://aspnetresources.com/blog/stripping_span_from_webcontrol
参考:http: //aspnetresources.com/blog/stripping_span_from_webcontrol
回答by Pedro
I spent the last 3 hours pulling my hair to find a solution at this problem.
在过去的 3 个小时里,我一直在努力寻找解决这个问题的方法。
Here is what came out:
这是出来的:
using System.Web.UI;
using System.Web.UI.WebControls;
/// <summary>
/// Represents a custom checkbox web control.
/// Prevents itself to be wrapped into a <span> tag when disabled.
/// </summary>
public class CustomCheckBox : CheckBox
{
/// <summary>
/// Renders the control to the specified HTML writer.
/// </summary>
/// <param name="writer">The HtmlTextWriter object that receives the control content.</param>
protected override void Render(HtmlTextWriter writer)
{
// Use custom writer
writer = new HtmlTextWriterNoSpan(writer);
// Call base class
base.Render(writer);
}
}
Along with the custom control, you'll need a custom HtmlTextWriter:
除了自定义控件,您还需要一个自定义 HtmlTextWriter:
using System.IO;
using System.Web.UI;
/// <summary>
/// Represents a custom HtmlTextWriter that displays no span tag.
/// </summary>
public class HtmlTextWriterNoSpan : HtmlTextWriter
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="textWriter">Text writer.</param>
public HtmlTextWriterNoSpan(TextWriter textWriter)
: base(textWriter)
{ }
/// <summary>
/// Determines whether the specified markup element will be rendered to the requesting page.
/// </summary>
/// <param name="name">Name.</param>
/// <param name="key">Tag key.</param>
/// <returns>True if the markup element should be rendered, false otherwise.</returns>
protected override bool OnTagRender(string name, HtmlTextWriterTag key)
{
// Do not render <span> tags
if (key == HtmlTextWriterTag.Span)
return false;
// Otherwise, call the base class (always true)
return base.OnTagRender(name, key);
}
}
Just FYI, using:
仅供参考,使用:
checkbox.InputAttributes.Add("disabled", "disabled");
has the same effect but:
具有相同的效果,但:
- It's not as convenient as checkbox.Enalbed = false;
- The attribute is removed after a postback when the checkbox is in a ListView.
- 不如 checkbox.Enalbed = false; 方便
- 当复选框位于 ListView 中时,在回发后删除该属性。
回答by Andres A.
I've found that by implementing a constructor like the one below, you can specify the container tag for your control.
我发现通过实现如下所示的构造函数,您可以为控件指定容器标记。
public MyCustomControl() : base(HtmlTextWriterTag.Div)
{
}
You can replace the HtmlTextWriterTag
with any of the aviable options, such as Div in the example above. The default is Span
.
您可以将 替换为HtmlTextWriterTag
任何可用的选项,例如上面示例中的 Div。默认为Span
。
回答by user2553451
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
should do
应该做