扩展 ASP.NET 面板

发布于 2024-12-03 03:29:01 字数 2413 浏览 1 评论 0原文

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

然而,每当我使用面板时,div 等都会正确渲染。 但框中的输入仅为:[标题]

即:如果我这样做:

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

}

I am trying to create a new Service Control extending the ASP.NET panel.

Howwever whenever I use my panel, the divs etc render correctly.
But the input in the box is only : [title]

ie: if i do:

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

all that is displayed is:
[MyBox]

(in a box that is correct)

This is my code:

[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");

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

檐上三寸雪 2024-12-10 03:29:01

如果您要继承 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 + "]";
        }

不幸的是,没有“正常”方法来获取面板的内部文本,但这是一种解决方法:

        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 数据时输出它。

If you would inherit Label, you would do it this way:

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

Unfortunately there is no "normal" way to get the inner text of a Panel, but this is a workaround:

        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 + "]";
        }

What you basically do is, you take the text in your Panel (which automatically gets inserted into a Literal child control by ASP.NET) and output it if there is no ViewState data.

一影成城 2024-12-10 03:29:01

有一种更简单的方法可以做到这一点。

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

例如:

.roundedbox {
    border-radius: 3px;
}

There is a much easier way to do this.

  • Create your control and have it extend Panel.
  • In it's constructor or in the OnInit() method, set the CssClass property to a roundedbox style (or any other name).
  • Add this style to your CSS and have it include border-radius.

For example:

.roundedbox {
    border-radius: 3px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文