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

时间:2013-09-12 11:55:17

标签: asp.net

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

5 个答案:

答案 0 :(得分:1)

使用反射器,您将看到每个控件都继承了使用标签“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);
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

使用它将起作用的属性..

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

答案 3 :(得分:0)

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

      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);
      }

我已尝试使用您的代码并且它可以正常工作! 我希望能帮到你,

答案 4 :(得分:0)

您还可以确保您控制的宽度和高度为空(如果可能),因为span和anchor是没有高度或宽度的内联控件,这就是WebControl显示更改为内联的原因 - 阻止高度或宽度适用于usercontrol。

如果你的控件需要高度和宽度,你可以使用它作为构造函数并声明标签有一个DIV

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