Using UpdatePanels Inside A Repeater On A Content Page

In my previous post, I described how to use an UpdatePanel that is located inside a Repeater or other data-bound control while maintaining your databinding. In addition to the data-binding challenge, dynamically creating UpdatePanels your page can also pose other complications.

Another issue that I’ve encountered is identifying controls that the AsyncPostBackTrigger on the UpdatePanel refer to if you are using master pages. So long as the control you are trying to trigger on is located in the same Content control of the content page, everything works just fine. However, if the UpdatePanel is located in one Content control while the control that triggers the asynchronous postback is in another UpdatePanel, you’ll get an exception when the page is loaded saying that it couldn’t find a control with the ID you specified.

I’ve found various solutions for correcting this problem by dynamically adding the trigger in code, and using the control’s UniqueID to identify it. However, this becomes significantly more complicated when you are dealing with a dynamically added control. The best solution I’ve found so far is to use ScriptManager.RegisterAsyncPostBackControl to register the post back, and manually call Update on each UpdatePanel to perform the update. Below is the code that I’ve found fixes the problem.

Partial Public Class _Default
Inherits System.Web.UI.Page

Dim DateUpdatePanels AS New List(Of UpdatePanel)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(edtDate)
DataBind()
End Sub

Private Sub Repeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater.ItemDataBound
Dim pnl As UpdatePanel = e.Item.FindControl(“upnlShowTimes”)
If Not IsNothing(pnl) Then
DateUpdatePanels.Add(pnl)
End If
End Sub

Private Sub edtDate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles edtDate.TextChanged
For Each pnl As UpdatePanel In DateUpdatePanels
pnl.Update()
Next
End Sub

End Class

Note that if you define your ScriptManager on your content page instead of the master page, you can refer to it directly instead of using ScriptManager.GetCurrent(Me). Also, you must be sure to set UpdateMode to Conditional on each UpdatePanel.

2 thoughts on “Using UpdatePanels Inside A Repeater On A Content Page”

  1. hello there! thanx for your post. i really am having a hard time figuring out what’s wrong in my code can you please help me sir? do you mind? by the way, my problem is that I have an UPDATEPANEL inside a REPEATER and this UPDATEPANEL contains another REPEATER and is TRIGGERED by a TIMER. Basically What I am trying to do is do an update every 5 seconds and checks the database if something has changed but the problem is that it instead returns to its old state. When I refresh the page the newly added item is there but when it updates after 5 seconds it rolls back. The newly added item is now gone. Hope you understood my question. If you want to see the code it’s here

  2. Janzen,

    Looking at just the aspx file I can’t say for certain what your problem might be. The first possibility that I’d look at is that the update panels inside the repeater aren’t been rendered by the page on the postback because the triggers you have in place aren’t mapping because they’re inside the repeater.

    I’d try using the ScriptManager.RegisterAsyncPostbackControl on the controls you currently have set as triggers as part of the ItemCreated event on the outer repeater. Then in the event handler for those controls that fire when they’re triggered on the server, call the Update method on the inner UpdatePanel to cause it to render itself. Let me know if that helps or not.

Leave a Reply