namespace My.Namespace
{
public class CustomEventArgs: EventArgs
{
public string SomeValue { get; set; }
}
public class MyServerControl : Control
{
public event
EventHandler<CustomEventArgs> CustomClick;
private void DoCustomClick(CustomEventArgs arg)
{
if (CustomClick != null)
{
CustomClick(this, arg);
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Page != null)
{
this.Page.ClientScript
.GetPostBackClientHyperlink(this, "");
if (this.Page.IsPostBack)
{
if (this.Page.Request.Form["__EVENTTARGET"]
== this.UniqueID)
{
CustomEventArgs args = new CustomEventArgs();
args.SomeValue =
this.Page.Request.Form["__EVENTARGUMENT"];
DoCustomClick(args);
}
}
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Href,
"javascript:__doPostBack('"
+ this.UniqueID + "', 'SomeValue');");
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.Write("Click here to do a postbak event with args!!!");
writer.RenderEndTag();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register Assembly="MyServerControl"
Namespace="My.Namespace" TagPrefix="MyTag" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Page</title>
<script runat="server">
protected void MyServerControl1_CustomClick(
object sender, CustomEventArgs e)
{
Response.Write(e.SomeValue);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<MyTag:MyServerControl ID="MyServerControl1" runat="server"
OnCustomClick="MyServerControl1_CustomClick" />
</div>
</form>
</body>
</html>
I`m not 100% sure that this is the way but for a quick implementation I think is ok to remember this.
