I can't seem to figure out how to fire off a javascript function from my code-behind.
Here is my javascript in my aspx page...
<script type="text/javascript">
...
function HidePopup() {
$(function() {
$("#popup").dialog("close");
});
});
I have a similar function called ShowPopup
that is fired for the OnClientClick
event of a LinkButton. That IS working. However, at the end of a function that is kicked off by pressing this button, I want to hide the JQuery dialog. I have tried the following methods...
ScriptManager.RegisterStartupScript(Me, Me.GetType, "HidePopup", "HidePopup();", True)
and
Page.ClientScript.RegisterStartupScript(Me.GetType(), "HidePopup", "HidePopup();")
I assume I need the first one (I'm making use of the AjaxControlToolkit and this particular button and <div>
is in an update panel) but neither one seems to be hiding the dialog box. I get no errors, so I'm kind of lost here. Any ideas? Need more information? Thanks in advance.
EDIT: So my issue isn't really that the javascript isn't firing off, I put in an alert(message)
in my HidePopup()
function that is going off, so my issue really just seems to be with this line...
$("#popup").dialog("close");
My div
is defined as...
<div id="popup" style="display:none;"></div>
Does anybody see anything wrong with that?
Much thanks to you guys, I figured out the issue. In case anyone else stumbles around here...
Since I'm using UpdatePanel
I had to use ScriptManager.RegisterStartupScript(<UpdatePanelID>, <UpdatePanelID>.GetType(), "HidePopup", "HidePopup();")
- Make note of @VeselinVasilev's answer to use the name of the UpdatePanel in the function call.
Most importantly though, I had to modify my method that actually started the popup. It seems the DOM was losing the reference to the Dialog. I created a global variable and assigned it to the Dialog when it was created. I could then use that to show / hide the Dialog.
var popupDialog;
function ShowPopup(message) {
$("#popup").html(message);
popupDialog = $("#popup").dialog({
title: "Working",
modal: true,
dialogClass: "no-close"
});
};
function HidePopup() {
popupDialog.dialog("close");
};
If anyone sees a problem with this or has anymore questions definitely let me know.