ASPNET 自定义控件添加显示:内联块;



>我有一个继承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>

使用反射器,您将看到每个控件继承使用标记"span"呈现的Web控件,并且"a"将具有

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并在变量中获取宽度,并将控制宽度设置为空值,然后执行基数。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);
}

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

使用属性它将起作用。

Me.Attributes["style"] = "display:inline-block;";

由于缺乏更优雅的解决方案,您可以重写 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);
      }

我已经尝试过您的代码,它有效!希望对您有所帮助,

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

如果控件需要高度和宽度,则可以用作构造函数并声明标记具有 DIV

public HomeLink ()
: base(HtmlTextWriterTag.Div)
{
}

最新更新