支持在控件标签间包含子控件 WebControl

之前写过关于UserControl的文章http://www.cnblogs.com/coolkiss/archive/2010/09/07/1820467.html

但是manager说UserControl复用性不好,于是改用WebControl了,改的过程遇到一些问题。

其中最主要的问题就是控件标签间 包含子控件,或者asp.net 页面变量的问题。

 

我希望实现的功能是,在控件间可以包含子控件

<cc:TitleEx runat="server">

<asp:Literal ID="litStr" runat="server"/>

</cc:TitleEx>

 

同时还要满足

<cc:TitleEx runat="server">

<%="hi"%>

</cc:TitleEx>

 

同时也可以这样使用:

<cc:TitleEx runat="server">

<% Response.Write("str");%>

</cc:TitleEx>

 

   [ParseChildren(false), PersistChildren(true), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class TitleEx : WebControl
    {
        public TitleEx()
            : base(HtmlTextWriterTag.Div)
        {
        }

        #region Attribute
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CssStyle
        {
            get
            {
                String s = (String)ViewState["CssStyle"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["CssStyle"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CloseFunction
        {
            get
            {
                String s = (String)ViewState["CloseFunction"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["CloseFunction"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string DemoUrl
        {
            get
            {
                String s = (String)ViewState["DemoUrl"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["DemoUrl"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                if (this.HasControls())
                {
                    this.Controls.Clear();
                }
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string HelpUrl
        {
            get
            {
                String s = (String)ViewState["HelpUrl"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["HelpUrl"] = value;
            }
        }

        #endregion

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "divtitle");
            if (CssStyle != null)
            {
                string strStyle = CssStyle;
                writer.AddAttribute(HtmlTextWriterAttribute.Style, strStyle);
            }
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            this.AddAttributesToRender(writer);
            HtmlTextWriterTag tagKey = this.TagKey;
            if (tagKey != HtmlTextWriterTag.Unknown)
            {
                writer.RenderBeginTag(tagKey);
                writer.Write(GetOutputHtmlBegin());
            }
            else
            {
                writer.RenderBeginTag(this.TagName);
            }
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.Write(GetOutputHtmlEnd());
            base.RenderEndTag(writer);
        }

        #region get output html
        protected string GetOutputHtmlBegin()
        {
            StringBuilder sbHtml = new StringBuilder();
            sbHtml.AppendLine("<table width='100%' cellpadding=0 cellspacing=0 border=0 class='EMRTitle'>");
            sbHtml.AppendLine("<tr>");
            sbHtml.AppendLine("<td width='10%' align='left' class='EMRTitle'></td>");
            sbHtml.AppendLine("<td width='90%' align='left' class='EMRTitle'>");

            return sbHtml.ToString();
        }

        protected string GetOutputHtmlEnd()
        {
            StringBuilder sbHtml = new StringBuilder();
            sbHtml.Append("</td>");
            sbHtml.AppendLine("<td width=80 align='right' valign='top' nowrap>");
            if (!string.IsNullOrEmpty(DemoUrl) && DemoUrl.IndexOf("HTTP") == -1)
            {
                DemoUrl = "http://www.oooo.com/help/video/" + DemoUrl;
            }
            if (!string.IsNullOrEmpty(DemoUrl) && DemoUrl.Length > 0)
                sbHtml.AppendLine("<a href=\"javascript:\" onclick=\"javascript:pop('" + DemoUrl + "','',750,750);\">	<img src='https://img.oooo.com/ec/images/icon_viewdemo.gif' border=0 title='View Online Demo' style='visibility: visible;'/></a>");
            if (!string.IsNullOrEmpty(HelpUrl) && HelpUrl.Length > 0)
                sbHtml.AppendLine("<a href=\"javascript:\" onclick=\"javascript:pop('" + HelpUrl + "','',750,750);\">	<img src='https://img.oooo.com/ec/images/icon_help_small.gif' border=0 title='View help document' style='visibility: visible;'/></a>");

            if (!string.IsNullOrEmpty(CloseFunction) && CloseFunction.Length > 0)
            {
                sbHtml.AppendLine("  <a href=\"javascript:" + CloseFunction + "\"><img src='https://img.oooo.com/ec/images/button_close.gif' border=0 title='Close'></a>");
            }
            sbHtml.AppendLine("</td>");
            sbHtml.AppendLine("</tr>");
            sbHtml.AppendLine("</table>");
            return sbHtml.ToString();
        }
        #endregion
    }

posted @ 2010-12-30 11:40  Chris Cheung  阅读(474)  评论(0编辑  收藏  举报