使用自定义按钮控件时,单击事件未触发
本文关键字:事件 单击 自定义 按钮 控件 | 更新日期: 2023-09-27 18:06:46
我已经做了自定义按钮控件,但是我错过了一些东西,因为我不能让它正常工作。
按钮工作,如果渲染方法像这样:
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
但是当我把代码改成这样:
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, css);
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ID);
writer.RenderBeginTag(HtmlTextWriterTag.Button);
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.Write(base.Text);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
它只是加载点击按钮的同一页面,其他什么都不发生。
所以我没有做什么base。render (write)做什么?在这些自定义控制按钮中还有什么应该做的,或者我能做得更好吗?
按钮输出应该像这样:
<button>
<span>
<span>
<span>
Title
</span>
</span>
</span>
</button>
您应该调用基方法…
base.Render(writer);
如下↓
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, css);
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ID);
writer.RenderBeginTag(HtmlTextWriterTag.Button);
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.Write(base.Text);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
base.Render(writer);
}