Custom WebControl using jQuery that needs to work inside an UpdatePanel
I have created a custom server side WebControl. 开发者_C百科 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!
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.
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);
}
精彩评论