Example 1: This control postback with the __EVENTTARGET, which is equal UniqueID of the control. This picks up the RaisePostBackEvent method automatically.
public class CustomButton : WebControl, IPostBackEventHandler { public event EventHandler Click; protected override void RenderContents(HtmlTextWriter writer) { writer.Write(string.Format( @"<input type=""button"" name=""{0}"" id=""{1}"" value=""Click"" onclick=""javascript:__doPostBack('{0}','')"" />", this.UniqueID, this.ClientID)); } public void RaisePostBackEvent(string eventArgument) { if (this.Click != null) this.Click(this, new EventArgs()); } }Example 2: This control postback with the __EVENTTARGET which is NOT equal to UniqueID of the control. So RaisePostBackEvent will NOT get raised automatically. Thus we need to call Page.RegisterRequiresRaiseEvent explicitly. However this approach does not pick up the eventArgument from the postback call.
public class ComplexButton : WebControl, IPostBackEventHandler { public event EventHandler Click; public void RaisePostBackEvent(string eventArgument) { if (this.Click != null) this.Click(this, new EventArgs()); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.Page.RegisterRequiresRaiseEvent(this); } protected override void RenderContents(HtmlTextWriter writer) { writer.Write("<div>"); writer.Write(string.Format( @"<input type=""button"" name=""{0}"" id=""{1}"" value=""Save"" onclick=""javascript:__doPostBack('{0}','')"" />", this.UniqueID + "$Name", this.ClientID + "_Name")); writer.Write(string.Format( @"<input type=""button"" name=""{0}"" id=""{1}"" value=""Edit"" onclick=""javascript:__doPostBack('{0}','')"" />", this.UniqueID + "$Age", this.ClientID + "_Age")); writer.Write("</div>"); } }
Example 3: If you need to map different buttons inside the same web control to different event, it is better to use same name as the custom controls UniqueID for the __EVENTTARGET and use eventArgument to distinguish which button get clicked.
public class ComplexButton : WebControl, IPostBackEventHandler { public event EventHandler Save; public event EventHandler Edit; public void RaisePostBackEvent(string eventArgument) { if (string.IsNullOrEmpty(eventArgument)) return; if (eventArgument.Equals("Save")) { if (this.Save != null) this.Save(this, new EventArgs()); } else if (eventArgument.Equals("Edit")) { if (this.Edit != null) this.Edit(this, new EventArgs()); } } protected override void RenderContents(HtmlTextWriter writer) { writer.Write("<div>"); writer.Write(string.Format( @"<input type=""button"" name=""{0}"" id=""{1}"" value=""Save"" onclick=""javascript:__doPostBack('{0}','Save')"" />", this.UniqueID, this.ClientID + "_Name")); writer.Write(string.Format( @"<input type=""button"" name=""{0}"" id=""{1}"" value=""Edit"" onclick=""javascript:__doPostBack('{0}','Edit')"" />", this.UniqueID, this.ClientID + "_Age")); writer.Write("</div>"); } }
2 comments:
Nice, this is what happens for asp:button and asp:imagebutton controls when usersubmitbehavior attribute set to false and true(by default) .
Nice ,This is what happens to buuton and imagebutton controls when usersubmitbehavoir set false and true.
Post a Comment