延长ASP.净面板

本文关键字:ASP 延长 | 更新日期: 2023-09-27 18:07:47

我正在尝试创建一个扩展ASP的新服务控件。净面板。

然而,每当我使用我的面板,div等渲染正确。但是框中的输入只有:[title]

即:if I do:

<cc1:RoundedCornerBox id="MyBox" BoxWidth="100" BoxHeight="200"> This is the content that   should be displayeed </cc1:RoundedBox>

显示的全部内容是:(MyBox)

(在正确的方框中)

这是我的代码:

[DefaultProperty("Text")]
[ToolboxData("<{0}:RoundedCornerBox runat=server></{0}:RoundedCornerBox>")]
public class RoundedCornerBox : System.Web.UI.WebControls.Panel
{
public int BoxWidth { get; set; }
public int BoxHeight { get; set; }

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
    get
    {
        String s = (String)ViewState["Text"];
        return ((s == null) ? "[" + this.ID + "]" : s);
    }
    set
    {
        ViewState["Text"] = value;
    }
}
protected override void Render(HtmlTextWriter writer)
{
    base.Render(writer);
}
protected override void RenderContents(HtmlTextWriter output)
{
    output.Write(Text);
}

public override void RenderBeginTag(HtmlTextWriter writer)
{
    base.RenderBeginTag(writer);
    writer.Write("<div class='"roundedcornr_lt'"></div>'n");
    writer.Write("<div class='"roundedcornr_top'" style='"width:" + BoxWidth.ToString() + "px'"></div>'n");
    writer.Write("<div class='"roundedcornr_rt'"></div>'n");
    writer.Write("<div class='"clear'"></div>'n");
    writer.Write("<div class='"roundedcornr_lside'" style='"height:" + BoxHeight.ToString() + "px'"></div>'n");
    writer.Write("<div style='"width:" + BoxWidth.ToString() + "px; height:" + BoxHeight.ToString() + "px; background:white; float:left'">'n");

}
public override void RenderEndTag(HtmlTextWriter writer)
{
    base.RenderEndTag(writer);
    writer.Write("</div>'n");
    writer.Write("<div class='"roundedcornr_rside'" style='"height:" + BoxHeight.ToString() + "px'"></div>'n");
    writer.Write("<div class='"clear'"></div>'n");
    writer.Write("<div class='"roundedcornr_bl'"></div>'n");
    writer.Write("<div class='"roundedcornr_btm'" style='"width:" + BoxWidth.ToString() + "px'"></div>");
    writer.Write("<div class='"roundedcornr_br'"></div>");
    writer.Write("<div class='"clear'"></div>'n");
}

延长ASP.净面板

如果你想继承Label,你可以这样做:

        get
        {
            string result = (string) ViewState["Text"];
            if(result != null)
                return result;
            result = (string) base.Text;
            if (!string.IsNullOrEmpty(result))
                return result;
            return "[" + this.ID + "]";
        }

不幸的是,没有"正常"的方法来获取Panel的内部文本,但这是一个变通方法:

        get
        {
            string result = (string) ViewState["Text"];
            if(result != null)
                return result;
            result = ((LiteralControl) this.Controls[0]).Text;
            if (!string.IsNullOrEmpty(result))
                return result;
            return "[" + this.ID + "]";
        }

你基本上要做的是,你在面板中获取文本(它会自动插入到ASP.NET的Literal子控件中)并输出它,如果没有ViewState数据

有一种更简单的方法。

  • 创建控件并扩展Panel
  • 在其构造函数或OnInit()方法中,将CssClass属性设置为roundedbox样式(或任何其他名称)。
  • 将此样式添加到您的CSS中并包含border-radius
例如:

.roundedbox {
    border-radius: 3px;
}