我正在尝试创建一个扩展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");
}
答案 0 :(得分:0)
如果您继承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 + "]";
}
你基本上做的是,你获取Panel中的文本(它自动被ASP.NET插入到Literal子控件中),如果没有ViewState数据则输出它。
答案 1 :(得分:0)
有一种更简单的方法可以做到这一点。
Panel
。OnInit()
方法中,将CssClass
属性设置为roundedbox
样式(或任何其他名称)。border-radius
。例如:
.roundedbox {
border-radius: 3px;
}