Therefore it's possible that 'WebCustomControl12$NewButton' is sent to server instead of 'WebCustomControl12'. Then ASP.NET thinks button's RaisePostBackEvent method should be called. By setting a breakpoint at Page's RaisePostBackEvent method you
can check the value of sourceControl. It's RaisePostBackEvent method will be called.
Interesting, that seems to have done it. I guess I missed that little detail. It does explain why when I was debugging the code would run through OnLoad and CreateChildControls twice. Of course it doesn't explain why "return false;" is required when the
control is in an UpdatePanel, but hey, it works, so I'm not going to argue. :)
I do know that using an event handler would be easier, but I need the event "handled" before CreateChildControls and the only way I know of to do so is to use the RaisePostBackEvent. If you have a cleaner/better solution, I'd love to hear it! Thanks!
Allen Chen –...
All-Star
40943 Points
4949 Posts
Re: RaisePostBackEvent not called when control is in an UpdatePanel
Sep 23, 2009 08:13 AM|LINK
Hi,
The result is not caused by UpdatePanel but the code in CreateChildControls. What your code rendered is something like below:
onclick="__doPostBack('WebCustomControl12','NewButton');__doPostBack('WebCustomControl12$NewButton','')"
Therefore it's possible that 'WebCustomControl12$NewButton' is sent to server instead of 'WebCustomControl12'. Then ASP.NET thinks button's RaisePostBackEvent method should be called. By setting a breakpoint at Page's RaisePostBackEvent method you can check the value of sourceControl. It's RaisePostBackEvent method will be called.
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
{
base.RaisePostBackEvent(sourceControl, eventArgument);
}
By appending a "return false" could workaround this issue:
public class WebCustomControl1 : WebControl, INamingContainer, IPostBackEventHandler
{
private Button b;
protected override void CreateChildControls()
{
b = new Button();
b.ID = "NewButton";
b.UseSubmitBehavior = false;
b.Text = "Click Me";
b.Attributes["onclick"] = Page.ClientScript.GetPostBackEventReference(this, b.ID) + ";return false;";
Controls.Add(b);
}
public void RaisePostBackEvent(string eventArgument)
{
}
}
However, a better practise is to hook Button's click event:
public class WebCustomControl1 : WebControl, INamingContainer
{
private Button b;
protected override void CreateChildControls()
{
b = new Button();
b.ID = "NewButton";
b.UseSubmitBehavior = false;
b.Text = "Click Me";
b.Click += new EventHandler(b_Click);
Controls.Add(b);
}
void b_Click(object sender, EventArgs e)
{
}
}
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
ipshing
Member
51 Points
40 Posts
Re: RaisePostBackEvent not called when control is in an UpdatePanel
Sep 23, 2009 04:17 PM|LINK
Interesting, that seems to have done it. I guess I missed that little detail. It does explain why when I was debugging the code would run through OnLoad and CreateChildControls twice. Of course it doesn't explain why "return false;" is required when the control is in an UpdatePanel, but hey, it works, so I'm not going to argue. :)
I do know that using an event handler would be easier, but I need the event "handled" before CreateChildControls and the only way I know of to do so is to use the RaisePostBackEvent. If you have a cleaner/better solution, I'd love to hear it! Thanks!
Gatwick
Member
49 Points
39 Posts
Re: RaisePostBackEvent not called when control is in an UpdatePanel
Nov 24, 2011 08:29 AM|LINK
I wonder if you ultimately have found the solution for the issue.