Sunday, October 17, 2010

Page.RegisterRequiresRaiseEvent

When you implement IPostBackEventHanlder RaisePostBackEvent method get automatically get called only if you postback the form with _EVENTTARGET equal to UniqueID of the web control [__doPostBack(‘ctrl001$mybutton1’,’’)].  But if you need to develop complex postback enable control which will invoke the RaisePostBackEvent as a response to custom __EVENTTRAGETs [__doPostBack(‘custom_target’)] not the UniqueID of the control, then you can register the complex postback enable control with Page.RegisterRequiresRaiseEvent

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 : WebControlIPostBackEventHandler
{
    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(thisnew 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 : WebControlIPostBackEventHandler
{
    public event EventHandler Click;
    public void RaisePostBackEvent(string eventArgument)
    {
        if (this.Click != null)
            this.Click(thisnew 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 : WebControlIPostBackEventHandler
{
    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(thisnew EventArgs());
        }
        else if (eventArgument.Equals("Edit"))
        {
            if (this.Edit != null)
                this.Edit(thisnew 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:

Anonymous said...

Nice, this is what happens for asp:button and asp:imagebutton controls when usersubmitbehavior attribute set to false and true(by default) .

shathar khan said...

Nice ,This is what happens to buuton and imagebutton controls when usersubmitbehavoir set false and true.

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...