使用反射器,您将看到每个控件都继承了使用标签“span”和“a”呈现的 webcontrol
Protected Overridable Sub AddAttributesToRender(ByVal writer As HtmlTextWriter)
...
If (Me.TagKey = HtmlTextWriterTag.Span OrElse Me.TagKey = HtmlTextWriterTag.A) Then
Me.AddDisplayInlineBlockIfNeeded(writer)
End If
.....
End Sub
Friend Sub AddDisplayInlineBlockIfNeeded(ByVal writer As HtmlTextWriter)
Dim isEmpty As Boolean = False
If (Not Me.RequiresLegacyRendering OrElse Not MyBase.EnableLegacyRendering) Then
If (Me.BorderStyle = BorderStyle.NotSet) Then
Dim borderWidth As Unit = Me.BorderWidth
If (borderWidth.IsEmpty) Then
Dim height As Unit = Me.Height
If (height.IsEmpty) Then
Dim width As Unit = Me.Width
isEmpty = width.IsEmpty
End If
End If
End If
If (Not isEmpty) Then
writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "inline-block")
End If
End If
End Sub
因此,为了避免应用样式display:inline-block
,您有 2 个选项
1-将以下代码添加到您的 web.config 以强制执行旧版渲染
<system.web>
<xhtmlConformance mode="Legacy"/>
</system.web>
2-覆盖方法 AddAttributesToRender 并在变量中获取宽度并将控件宽度设置为空值然后执行 base.AddAttributesToRender(writer) 然后添加宽度(如果已设置)
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
Unit width = this.Width;
this.Width = new Unit();
base.AddAttributesToRender(writer);
if (!width.IsEmpty)
writer.AddStyleAttribute("width", width.ToString);
}