0

我有一个继承的自定义控件System.Web.UI.WebControls.WebControl

呈现控件时添加style="display:inline-block;"

我使用 Me.Style.Remove("display") Me.Style["display"]="something"和其他类似的东西,但这段代码仍然存在。

您可以在这个简单的控件中看到相同的行为:

    public class HomeLink : System.Web.UI.WebControls.WebControl {
    protected override void OnPreRender(EventArgs e) {
        this.Attributes["style"] = "aaaa";
        base.OnPreRender(e);
        this.Attributes["style"] = "bbb";
    } }

而这背后的代码:

    <FC:HomeLink ID="HomeLink1" runat="server" width="100px" />

并像这样渲染:

    <span id="HomeLink1" style="display:inline-block;width:100px;bbb"></span>
4

5 回答 5

2

使用反射器,您将看到每个控件都继承了使用标签“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);
}
于 2014-03-05T13:57:56.130 回答
0

您还可以确保控件的宽度和高度为空(如果可能的话),因为跨度和锚点是没有高度或宽度的内联控件,这就是为什么将 WebControl 显示更改为内联块以便应用高度或宽度到用户控件。

如果您的控件需要高度和宽度,您可以将其用作构造函数并声明标签具有 DIV

public HomeLink ()
: base(HtmlTextWriterTag.Div)
{
}
于 2014-04-29T18:19:24.563 回答
0

Style 类中没有 Remove() 方法,请查看此MSDN链接。

于 2013-09-12T12:03:44.610 回答
0

使用属性它将起作用..

Me.Attributes["style"] = "display:inline-block;";
于 2013-09-12T12:11:22.003 回答
0

缺少更优雅的解决方案,您可以覆盖 render 方法:

      protected override void Render(System.Web.UI.HtmlTextWriter writer)
      {
          //Create a new HtmlTextWriter to render the original control output
          var sb = new System.Text.StringBuilder();
          System.IO.TextWriter textWriter = new System.IO.StringWriter(sb);
          System.Web.UI.HtmlTextWriter preWriter = new System.Web.UI.HtmlTextWriter(textWriter);
          base.Render(preWriter);

          //Here you can modify any output content
          sb.Replace("display:inline-block;", "");

          //Finally write control writer
          writer.Write(sb);
      }

我已经尝试过您的代码,它可以工作!希望能帮到你

于 2013-09-26T15:44:42.417 回答