一个简单的自定义控件的事件处理示例 [IPostBackEventHandler]

代码
public class ClickEventArgs : System.EventArgs
{
private string _cuPg;

public ClickEventArgs(string arg)
{
_cuPg
= arg;
}

public string CuPg
{
get { return _cuPg; }
}
}

public delegate void SClickDelegate(object sender, ClickEventArgs e);

public class TC : System.Web.UI.Control, IPostBackEventHandler
{
public event SClickDelegate SClick;

private string _currentArg = string.Empty;

public string CurrentArg
{
get { return _currentArg; }
set { _currentArg = value; }
}

public TC()
{
}

protected override void RenderChildren(HtmlTextWriter writer)
{
base.RenderChildren(writer);

for(int i=0; i <10; i++)
{
writer.AddAttribute(HtmlTextWriterAttribute.Name,
this.UniqueID + "_CustomerControlBndy");
writer.RenderBeginTag(HtmlTextWriterTag.A);
if (i.ToString() == _currentArg)
{
writer.AddAttribute(HtmlTextWriterAttribute.Disabled,
"true");
}
else
{
writer.AddAttribute(HtmlTextWriterAttribute.Href,
this.Page.GetPostBackClientHyperlink(this, i.ToString()));
}

writer.Write(i.ToString());
writer.RenderEndTag();
writer.WriteBreak();
}
}

public void OnSClick(ClickEventArgs e)
{
if (this.SClick != null)
{
_currentArg
= e.CuPg;
SClick(
this, e);
}
}


#region IPostBackEventHandler 成员

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

protected virtual void RaisePostBackEvent(string arg)
{
this.OnSClick(new ClickEventArgs(arg));
}

#endregion
}

 

posted @ 2010-03-26 13:51  bndy  阅读(278)  评论(0编辑  收藏  举报