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

本文探讨了自定义Web服务器控件时,基类方法调用顺序对控件行为的影响。通过一个示例,展示了在`AddAttributesToRender`和`RenderContents`方法中,基类方法调用位置的不同导致的输出差异。作者指出,正确理解并掌握这些细节对于开发高效、稳定的Web控件至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做一个简单的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 = "这是一个简单的Web控件";

string html = "";

for (int i = 1; i < 6; i++)

{

html += string.Format(template, i);

}

output.Write(html);

}

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值