gpt4 book ai didi

c# - 扩展 ASP.NET 面板

转载 作者:太空宇宙 更新时间:2023-11-03 16:41:47 24 4
gpt4 key购买 nike

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

但是无论何时我使用我的面板,div 等都会正确呈现。但是框中的输入只有:[title]

即:如果我这样做:

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

所有显示的是: [我的盒子]

(在正确的框中)

这是我的代码:

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

}

最佳答案

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

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

关于c# - 扩展 ASP.NET 面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7298130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com