4

I have created a custom server side WebControl. This control calls an initialization script that uses jQuery to hookup the events with calls to bind when the page is loaded.

This control is now being used inside an UpdatePanel and obviously the client side events no longer exist after the UpdatePanel does it's thing. So, I need to re-run my initialization script if the control has been re-rendered as part of a partial page refresh and I don't see a good way of doing this.

I am aware of the ScriptManager.IsInAsyncPostBack and UpdatePanel.IsInPartialRendering, but they don't seem to provide what I need. It seems to implement this correctly that I will have to check if ScriptManager.IsInAsyncPostBack==true, then search up the control tree for an UpdatePanel that has IsInPartialRendering==true. If I find such an UpdatePanel then I re-run my initialization script.

Sounds horrible. Am I missing something simple? I can't be the only one who lives this way.

Thanks for reading!

internet man
  • 840
  • 1
  • 9
  • 21

2 Answers2

2

Have you tried using the method described in the SO post below? jQuery $(document).ready and UpdatePanels?

This is what I do when I use jQuery in update panels and it always works for me.

Community
  • 1
  • 1
ryanulit
  • 4,983
  • 6
  • 42
  • 66
  • My case is more complex than that. I am within a custom WebControl that needs to work in all cases and may appear multiple times on a single page. I think to handle it like that in the pageRequestManager that I would have to detach all existing handlers and then attach all of them again. I guess this is doable, but I was hoping for something cleaner. – internet man May 26 '10 at 13:46
0

Have to do what I was afraid of... also, Update.IsInPartialRendering does not work, so you have to use reflection to figure out if the updatePanel is getting updated. So, if IsControlBeingRendered is true, then run your scripts.

       public static bool IsControlBeingRendered(ScriptManager scriptManager, Control control)
        {

          if (scriptManager.SupportsPartialRendering && scriptManager.IsInAsyncPostBack)
          {
            UpdatePanel updatePanel = ControlHelper.FindParentByType<UpdatePanel>(control);

            while (updatePanel != null)
            {
              if (IsBeingUpdated(updatePanel))
              {
                return true;
              }
              else
              {
                updatePanel = ControlHelper.FindParentByType<UpdatePanel>(updatePanel);
              }
            }
return false;
          }

          return true;
        }


    public static bool IsBeingUpdated(UpdatePanel updatePanel)
    {
      // unfortunately, updatePanel.IsInPartialRendering does not work.  So, we must use reflection
      // to check the protected property that actually does work..

      if (updatePanel == null)
        return false;

      Type type = updatePanel.GetType();
      BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty;

      return (bool)type.InvokeMember("RequiresUpdate", bindingFlags, null, updatePanel, null);
    }
internet man
  • 840
  • 1
  • 9
  • 21
  • This doesn't always work! It is flakey when using an updatePanel with updatemode=conditional. How do custom control developers handle this situation?!?! – internet man Jul 21 '10 at 14:11
  • It seems to be something like the RequiresUpdate property isn't correct when UpdateMode=Conditional unless Update() is explicitly called, which it should be. – internet man Jul 21 '10 at 17:46