private void ChangeVisibility(bool show)
{
if(TargetControl == null)
throw new ArgumentNullException("TargetControl", "TargetControl property cannot be null");
var operation = show ? "show" : "hide";
if(ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
// RegisterDataItem is more elegant, but we can only call it during an async postback
ScriptManager.GetCurrent(Page).RegisterDataItem(TargetControl, operation);
else {
// Add a load handler to show the popup and then remove itself
var script = string.Format(CultureInfo.InvariantCulture,
"(function() {{" +
"var fn = function() {{" +
"Sys.Extended.UI.ModalPopupBehavior.invokeViaServer('{0}', {1}); " +
"Sys.Application.remove_load(fn);" +
"}};" +
"Sys.Application.add_load(fn);" +
"}})();",
BehaviorID,
show ? "true" : "false");
ScriptManager.RegisterStartupScript(this, typeof(ModalPopupExtender), operation + BehaviorID, script, true);
}
}