开发简单Web服务器控件碰到的问题-何时调用基类方法?

做一个简单的Web服务器控件:变色的Label

 public class MyLabel : Label
    {
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {

            this.Attributes.Add("onmouseover", "style.color='#ff0000';style.cursor='pointer';");
            this.Attributes.Add("onmouseout", "style.color='#000000';style.cursor='default';");
            base.AddAttributesToRender(writer);
        }
    }

运行正常。如果把:base.AddAttributesToRender(writer);这句移到前面,即:

            base.AddAttributesToRender(writer);

           this.Attributes.Add("onmouseover", "style.color='#ff0000';style.cursor='pointer';");
            this.Attributes.Add("onmouseout", "style.color='#000000';style.cursor='default';");

就发现没效果了。不明白,为什么base.AddAttributesToRender(writer);这句只能放后面?

而另外一个简单控件:

        protected override void RenderContents(HtmlTextWriter output)
        {
            base.RenderContents(output);
            string template = "<h{0}>这是一个简单的Web控件</h{0}>";
            string html = "";
            for (int i = 1; i < 6; i++)
            {
                html += string.Format(template, i);
            }
            output.Write(html);
        }

这句base.RenderContents(output);却要放在前面?

开发简单Web服务器控件时 ,究竟应该何时调用基类方法?

蛊毒传说
浏览 222回答 1
1回答

qq_花开花谢_0

   base.AddAttributesToRender(writer); 我考虑应该是这句话就是将属性值输出到HTML字符串输出流里面的。你再这个句话的后面添加,那就不会有效了!仅供参考!
打开App,查看更多内容
随时随地看视频慕课网APP