这是一个选择省份然后将点击事件上传到控件顶层在页面处理事件的一个例子。加粗部分是整个上传事件过程。上传事件(暴露事件)、暴露属性、复杂属性管理、样式管理等是asp.net服务器自定义控件的高级主题。
using system;
using system.web;
using system.web.ui;
using system.data;
using system.data.sqlclient;
using system.web.ui.webcontrols;
using system.componentmodel;
using yeno.enbiz.news.tools;
namespace yeno.enbiz.news.controls
{
/// <summary>
/// researchctrl 的摘要说明。
/// </summary>
[toolboxdata("<{0}:localctrl_province runat=server></{0}:localctrl_province>")]
public class localctrl_province : system.web.ui.webcontrols.webcontrol,inamingcontainer
{
private tablecell _province;
public event commandeventhandler clickprovince;
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 html 编写器 </param>
protected override void render(htmltextwriter writer)
{
addattributestorender(writer);
writer.addattribute(htmltextwriterattribute.cellpadding,"5",false);
writer.addattribute(htmltextwriterattribute.cellspacing,"0",false);
writer.addattribute(htmltextwriterattribute.width,"100%",false);
writer.renderbegintag(htmltextwritertag.table);
writer.renderbegintag(htmltextwritertag.tr);
_province.rendercontrol(writer);
writer.renderendtag();
writer.renderendtag();
}
protected override void createchildcontrols()
{
if(!page.ispostback)
{
controls.clear();
clearchildviewstate();
}
_province=new tablecell();
createprovince();
this.controls.add(_province);
}
protected void createprovince()
{
datatable dtb=(datatable)datacall.runsqlsp("n_province_getall",returntype.executereader);
for(int i=0;i<dtb.rows.count;i++)
{
linkbutton lb=new linkbutton();
lb.text=dtb.rows[i]["province"].tostring().trim();
lb.causesvalidation=false;
lb.commandargument=dtb.rows[i]["id"].tostring();
lb.command +=new commandeventhandler(province_command);
_province.controls.add(lb);
_province.controls.add(new literalcontrol(" "));
}
dtb.dispose();
}
protected void province_command(object sender,commandeventargs e)
{
onclickprovince(e);
}
protected void onclickprovince(commandeventargs e)
{
if(clickprovince!=null)
{
clickprovince(this,e);
}
}
}
}
