RaisePostBackEvent not called when control is in an UpdatePanelhttp://forums.asp.net/t/1471516.aspx/1?RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelThu, 24 Nov 2011 08:29:01 -050014715163409509http://forums.asp.net/p/1471516/3409509.aspx/1?RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRaisePostBackEvent not called when control is in an UpdatePanel <p>&nbsp;I have followed the samples online and successfully called RaisePostBackEvent, but now that I've moved my control inside an UpdatePanel the method is no longer called. Is there something else that needs to be done? Basically I've modified as simply as possible the example found on MSDN: <a href="http://msdn.microsoft.com/en-us/library/ms153112.aspx"> http://msdn.microsoft.com/en-us/library/ms153112.aspx</a>.</p> <p>&nbsp;</p> <p>Please help!!!</p> 2009-09-17T00:52:52-04:003409777http://forums.asp.net/p/1471516/3409777.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>Have you registered your control for postback, using RegisterRequiresPostBack();<br> </p> 2009-09-17T05:13:40-04:003411044http://forums.asp.net/p/1471516/3411044.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>Actually, I have tried that (as well as RegisterRequiresRaiseEvent) and neither work. I don't believe that either of those methods are related to this issue though. Here is the control I have:</p> <p>public class WebCustomControl1 : WebControl, INamingContainer, IPostBackEventHandler<br> {<br> &nbsp;&nbsp;&nbsp; private Button b;<br> <br> &nbsp;&nbsp;&nbsp; protected override void CreateChildControls()<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = new Button();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.ID = &quot;NewButton&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.UseSubmitBehavior = false;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.Text = &quot;Click Me&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.Attributes[&quot;onclick&quot;] = Page.ClientScript.GetPostBackEventReference(this, b.ID);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Controls.Add(b);<br> &nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp; public void RaisePostBackEvent(string eventArgument)<br> &nbsp;&nbsp;&nbsp; {<br> <br> &nbsp;&nbsp;&nbsp; }<br> }</p> <p>Like I said, when I place the control on the page <em>outside</em> an UpdatePanel, it works fine. But as soon as I place it in an UpdatePanel, it no longer calls RaisePostBackEvent and I can't figure out why.</p> 2009-09-17T15:35:47-04:003412079http://forums.asp.net/p/1471516/3412079.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>FOLLOW <a href="/t/1471558.aspx">THIS </a>LINK AND TEST YOUR CONTROL<br> </p> 2009-09-18T04:42:30-04:003412118http://forums.asp.net/p/1471516/3412118.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>&nbsp;I don't mean to sound rude, but I just don't think you are understanding what I need. The code you linked me to is not helpful to my situation. First, as I've stated several times, I need the code to work in an <strong>UpdatePanel</strong>. Secondly, calling &quot;Page.RegisterRequiresRaiseEvent is not useful and really shouldn't be used in most cases as it will always cause &quot;eventArgument&quot; in the &quot;RaisePostBackEvent&quot; method to be null. Which leads to number three, the clicked child control will never actually call RaisePostBackEvent because it's being overshadowed by the &quot;Page.RegisterRequiresRaiseEvent&quot;.</p> <p>I do hope that you have other suggestions about how to get RaisePostBackEvent called when the control is in an <strong>UpdatePanel</strong>, I really need a solution to this and am willing to look at any options.</p> <p>(As a side note, the problem in the post&nbsp;you linked could have been much more easily solved by implementing INamingContainer in the original WebControl.)</p> 2009-09-18T05:09:43-04:003412155http://forums.asp.net/p/1471516/3412155.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p></p> <blockquote><span class="icon-blockquote"></span> <h4>ipshing</h4> &nbsp;I don't mean to sound rude, but I just don't think you are understanding what I need. The code you linked me to is not helpful to my situation. First, as I've stated several times, I need the code to work in an <b>UpdatePanel</b>. </blockquote> <p></p> <p>AFAIK, there are no two ways of designing a control one that works with update panels and the other without updatepanel, updatepanel is just a wrapper which posts back using XmlHttp.</p> <p><br> </p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>ipshing</h4> (As a side note, the problem in the post&nbsp;you linked could have been much more easily solved by implementing INamingContainer in the original WebControl.)</blockquote> <br> <p></p> <p>How and where is that going to help? u mean i don't need to expose any event?<br> </p> 2009-09-18T05:30:34-04:003412191http://forums.asp.net/p/1471516/3412191.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p></p> <blockquote><span class="icon-blockquote"></span> <h4>shashankgwl</h4> AFAIK, there are no two ways of designing a control one that works with update panels and the other without updatepanel, updatepanel is just a wrapper which posts back using XmlHttp.</blockquote> &nbsp; <p></p> <p>I agree that putting it in an UpdatePanel should not make a difference. That's why I'm so frustrated. Here is my control:</p> <pre class="prettyprint">public class CustomControl : WebControl, INamingContainer, IPostBackEventHandler { private Button button; protected override void CreateChildControls() { button = new Button(); button.ID = &quot;Button1&quot;; button.UseSubmitBehavior = false; button.Text = &quot;Click Me&quot;; button.Attributes[&quot;onclick&quot;] = Page.ClientScript.GetPostBackEventReference(this, button.ID); Controls.Add(button); } public void RaisePostBackEvent(string eventArgument) { } }</pre> <P><BR>Now here is the the first example of the aspx side:</P><pre class="prettyprint"> &lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;&lt;/asp:ScriptManager&gt; &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt; &lt;ContentTemplate&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;cc1:CustomControl ID="CustomControl1" runat="server" /&gt; &lt;/form&gt; </pre> <P><BR>When I run the above code, <STRONG>RaisePostBackEvent</STRONG> is called and <STRONG>eventArgument</STRONG> is equal to the ID of my button. However, when I move the control like this:</P><pre class="prettyprint"> &lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;&lt;/asp:ScriptManager&gt; &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt; &lt;ContentTemplate&gt; &lt;cc1:CustomControl ID="CustomControl1" runat="server" /&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/form&gt; </pre> <p><strong>RaisePostBackEvent</strong> is never called. Putting it in an UpdatePanel shouldn't make a difference but it does. I need the control to be in an UpdatePanel so hopefully there is a solution out there that will work for me.</p> 2009-09-18T05:52:41-04:003412196http://forums.asp.net/p/1471516/3412196.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>&nbsp;</p> <blockquote><span class="icon-blockquote"></span> <h4>shashankgwl</h4> How and where is that going to help? u mean i don't need to expose any event?</blockquote> <p></p> <p>I made a response to that post you linked earlier. It would probably be easier to just view my response there rather than try and explain it in this thread (since the two aren't really related).</p> 2009-09-18T05:54:15-04:003412277http://forums.asp.net/p/1471516/3412277.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>You wan't to call&nbsp;<font size="2">RaisePostBackEvent without calling RegisterRequiresPostBack, i don't know how's that possible, and how it worked earlier</p> </font> 2009-09-18T06:35:48-04:003413073http://forums.asp.net/p/1471516/3413073.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>&nbsp;</p> <blockquote><span class="icon-blockquote"></span> <h4>shashankgwl</h4> You wan't to call&nbsp;<font size="2">RaisePostBackEvent without calling RegisterRequiresPostBack, i don't know how's that possible, and how it worked earlier</font></blockquote> <p>You seem to be under the impression that the two (<strong>RegisterRequiresPostBack</strong> and <strong>RaisePostBackEvent</strong>) are dependant on each other when that's simply not true at all. While registering your control with <strong>RegisterRequiresPostBack</strong> does cause <strong>RaisePostBackEvent</strong> to get fired, as I stated before, eventArgument will always be null. You can also get <strong>RaisePostBackEvent</strong> to be fired without calling <strong>RegisterRequiresPostBack</strong> (see this example here for proof: <a href="http://msdn.microsoft.com/en-us/library/ms153112.aspx">http://msdn.microsoft.com/en-us/library/ms153112.aspx</a>). </p> <p>Again, as I stated before, <strong>Page.RegisterRequiresPostBack</strong> is not ideal in most situations (there is ample documentation on the internet to back me up on this one). If you would just create the control I exampled above you would see what I am talking about and understand what I'm saying.</p> <p></p> <p>In my experience there is often more than one solution to a problem&nbsp;and repeatedly attempting a solution that is known to be wrong is never helpful. So please, shashankgwl, until you have a solution that doesn't involve <strong>RegisterRequiresPostBack</strong> (unless you can find one that works with it), perhaps let someone else provide a suggestion? I don't want to be rude, but I would much rather find a solution to my problem than debate whether or not your method is valid.</p> <p>Thanks.</p> 2009-09-18T14:08:33-04:003420295http://forums.asp.net/p/1471516/3420295.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>&nbsp;Hi,</p> <p>The result is not caused by UpdatePanel but the code in CreateChildControls. What your code rendered is something like below:</p> <p>&nbsp;</p> <p><span>onclick</span>=<span>&quot;__doPostBack('WebCustomControl12','NewButton');__doPostBack('WebCustomControl12&#36;NewButton','')&quot;</span></p> <p><span></span>&nbsp;</p> <p><span>Therefore it's possible that 'WebCustomControl12&#36;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.</span></p> <p>&nbsp;protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; base.RaisePostBackEvent(sourceControl, eventArgument);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>By appending a &quot;return false&quot; could workaround this issue:</p> <p>public class WebCustomControl1 : WebControl, INamingContainer, IPostBackEventHandler<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private Button b;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected override void CreateChildControls()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = new Button();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.ID = &quot;NewButton&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.UseSubmitBehavior = false;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.Text = &quot;Click Me&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.Attributes[&quot;onclick&quot;] = Page.ClientScript.GetPostBackEventReference(this, b.ID) &#43; &quot;;return false;&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Controls.Add(b);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void RaisePostBackEvent(string eventArgument)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;</p> <p>However, a better practise is to hook Button's click event:</p> <p>&nbsp;</p> <p>&nbsp;&nbsp; public class WebCustomControl1 : WebControl, INamingContainer<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private Button b;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected override void CreateChildControls()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = new Button();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.ID = &quot;NewButton&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.UseSubmitBehavior = false;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.Text = &quot;Click Me&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.Click &#43;= new EventHandler(b_Click);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Controls.Add(b);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void b_Click(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;</p> 2009-09-23T08:13:45-04:003421271http://forums.asp.net/p/1471516/3421271.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>&nbsp;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 &quot;return false;&quot; is required when the control is in an UpdatePanel, but hey, it works, so I'm not going to argue. :)</p> <p>I do know that using an event handler would be easier, but I need the event &quot;handled&quot; 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!</p> 2009-09-23T16:17:09-04:004698936http://forums.asp.net/p/1471516/4698936.aspx/1?Re+RaisePostBackEvent+not+called+when+control+is+in+an+UpdatePanelRe: RaisePostBackEvent not called when control is in an UpdatePanel <p>I wonder if you ultimately have found the solution for the issue.</p> 2011-11-24T08:29:01-05:00