0

Here's the situation: I have a variable number of dynamically created update panels on my page, so I thought I would write one method which handles all of the loading for each one.

My Updatepanel creation looks something like this:

 Dim newUpdp As New UpdatePanel
 newUpdatep.ChildrenAsTriggers = False
 newUpdatep.UpdateMode = UpdatePanelUpdateMode.Conditional
 newUpdatep.ID = Guid.NewGuid.ToString

 AddHandler newUpdatep.Load, AddressOf updatep_load_method
 updatep_Holder.ContentTemplateContainer.Controls.Add(newUpdp)
 updatep_Holder.Update()  

This creates the update panel and binds its load event to the method "updatep_load_method". This method is called as soon as the Updatepanel is inserted into the holder. The method code is as follows:

Private Sub updp_load_method(sender As UpdatePanel, e As System.EventArgs)
   Dim div As New HtmlGenericControl("div")
   div.InnerText = Date.Time.Now.ToString
   sender.ContentTemplateContainer.Controls.Add(div)
   sender.Update()
End Sub

A little later on I want to update the panel, and refresh the time. So I use the javascript __doPostBack method. According to Dave Ward, the __doPostBack method follows the full page postback lifecycle, so I figured the load event of my Update panel would be fired and that "updatep_load_method" would be called by that particular update panel...

Although the partial postback occurs, and other update panel's load events are called, my bound ones aren't. So what's happening here?

Mrchief
  • 75,126
  • 20
  • 142
  • 189
Andy F
  • 1,517
  • 2
  • 20
  • 35
  • Are you adding the update panel again in the post back? – Tejs Aug 31 '11 at 16:06
  • @Tejs, I wondered if that might be happening - like I might be recreating the controls but not binding them again or something like that. I will try James Johnson's answer tomorrow and see how I get on. – Andy F Aug 31 '11 at 17:35

2 Answers2

0

Try implementing the RaisePostBackEvent method in your code:

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    //call the RaisePostBack event 
    base.RaisePostBackEvent(source, eventArgument);

   //add whatever code you need to execute on postback here
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
0

You need to recreate dynamic controls during Page_Init and have to use the same Id (so you cannot do a Guid.NewGuid.ToString() while recreating the control).

Otherwise, ASP.Net will not be able to locate your control and populate it with data from ViewState.

Here's a good article that explains more about dynamic controls.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • Thanks @Mrchief, I will certainly give this a try. Have you any suggestions for retrieving the ID of the update panel so I can recreate with the same ID? Or would you suggest not using GUIDS in the first place? The only reason I'm using them is to achieve unique ids for each of the dynamically created panels. – Andy F Aug 31 '11 at 17:40
  • You need to keep track of the ID somehow. I would suggest you base it off some known property which you can use while recreating also. Its hard to tell without further insight into the code. If possible, avoid using dynamic controls and avoid using UpdatePanels altogether if you can! – Mrchief Aug 31 '11 at 17:49
  • I took your advice about not using dynamic panels - I found it easier to just pre-populate the page with some controls (so I know the IDs beforehand) and then re-bind the onload during page_init as you suggested. – Andy F Sep 03 '11 at 06:20