日期:2014-05-19  浏览次数:20486 次

webControl自定义控件事件如何调用?
webControl自定义控件事件如何调用?
我想要封装一些控件在一个自定义里头,如何写事件和页面互动?


------解决方案--------------------
继承IPostBackEventHandler,用RaisePostBackEvent注册

public delegate void PageIndexHandler(string pIndex);
[DefaultProperty( "Text ")]
[ToolboxData( " <{0}:LocalPageFootControl runat=server> </{0}:LocalPageFootControl> ")]
public class TestControl : WebControl,IPostBackEventHandler
{
public event PageIndexHandler PageIndexChange;
protected virtual void OnClick(string pIndex)
{
if (PageIndexChange != null)
PageIndexChange(pIndex);
}

void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
OnClick(eventArgument);
}

protected override void RenderContents(HtmlTextWriter output)
{
StringBuilder builder = new StringBuilder( " ");

builder.Append( " <input type=\ "button\ " class=\ "input_button\ " name=\ "index\ " " + i.ToString() + " value=\ " " + i.ToString() + "\ " OnClick=\ " " + this.Page.ClientScript.GetPostBackEventReference(this, i.ToString()) + "\ " /> ");
output.Write(builder.ToString());
}