c# - asp.net 动态样式缺少最小高度

标签 c# asp.net css

我正在尝试动态构建一个样式表(基于某些业务规则)长话短说,它大部分都有效,除了有一个 css 标记我无法设置最小高度。我可以达到高度,但不能达到最小高度。都是有效的 css 值

string h = "105mm";
Style dynamicClassStyle = new Style();
dynamicClassStyle.Height = new Unit(h);
Page.Header.StyleSheet.CreateStyleRule(dynamicClassStyle, null, ".make-display");

这样可以正确显示高度标签,但我真正需要的是最小高度。我希望能够去

dynamicClassStyle.Min-Height = new Unit(h);

为了得到这个

.make-display {
  min-height:90mm;
}

最佳答案

你需要

  1. 继承自Style ,
  2. 添加一个MinHeight属性和
  3. 覆盖FillStyleAttributes处理您的新属性(property)。
  4. 然后您可以在自己的代码中使用这个新的(最小高度感知)MinHeightStyle(而不是 Style)的实例。

新类:

using System.Web.UI;
using System.Web.UI.WebControls;

public class MinHeightStyle : Style
{
    public Unit MinHeight
    {
        get
        {
            var minHeight = this.ViewState["MinHeight"];

            if (minHeight != null)
            {
                return (Unit)minHeight;
            }

            return Unit.Empty;
        }

        set
        {
            this.ViewState["MinHeight"] = value;
        }
    }

    protected override void FillStyleAttributes(CssStyleCollection attributes, IUrlResolutionService urlResolver)
    {
        base.FillStyleAttributes(attributes, urlResolver);

        if (!this.MinHeight.IsEmpty)
        {
            attributes.Add("min-height", this.MinHeight.ToString());
        }
    }
}

还有你的新代码:

string h = "105mm";
MinHeightStyle dynamicClassStyle = new MinHeightStyle();
dynamicClassStyle.Height = new Unit(h);
dynamicClassStyle.MinHeight = new Unit(h);
Page.Header.StyleSheet.CreateStyleRule(dynamicClassStyle, null, ".make-display");

呈现以下内容:

.make-display { min-height:105mm;height:105mm; }

关于c# - asp.net 动态样式缺少最小高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18738529/

相关文章:

c# - 实现业务规则的规范模式

asp.net - Web 服务调用失败,出现 5xx 类型错误

c# - ASP.NET Razor Pages 检查属性验证状态?

html - 如何覆盖 Firefox 只读输入默认光标?

javascript 或 jquery,点击一个链接并展开一个 div

c# - 在具有特定值的字典中获取第一个 KeyValuePair

java - 发出发布请求时无法从 jsoup 获取结果

c# - 在 Azure 上使用 Codedom/CSharpCodeProvider 进行动态编译

c# - 如何在 WPF 和 ASP.NET 应用程序之间进行通信?

css - 使用 Bootstrap 固定页脚和内容填充器