问题
I am trying to open a jQuery UI Dialog from my ASP.NET c# Code Behind. I have created the code below, but I can't get it to work, and am unsure how to debug the problem. How can I resolve this issue?
My JavaScript:
<script>
$(function () {
$("#dialog_info").dialog("destroy");
$("#dialog_info").dialog({
autoOpen: false,
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
});
function show() {
$("#dialog_info").dialog("open");
return false;
}
</script>
My HTML:
<body>
<form id="form1" runat="server">
<div>
<div id="dialog_info" title="information">
<asp:Literal ID="ltMessage" runat="server" Text="success"></asp:Literal>
</div>
<a href="#" id="message">open</a>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
My Code Behind:
protected void Button1_Click(object sender, EventArgs e)
{
ltMessage.Text = DateTime.Now.ToString();
StringBuilder sb = new StringBuilder();
//sb.Append("<script> ");
sb.Append("show();");
//sb.Append("</script>");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key", sb.ToString(), true);
}
modified code
function showMessage() {
$(function () {
$("#dialog_info").dialog("open");
});
return false;
}
it works well now!
回答1:
Try use ScriptManager.RegisterStartupScript method
See Difference between RegisterStartupScript and RegisterClientScriptBlock?
来源:https://stackoverflow.com/questions/12704225/how-do-i-open-a-jquery-ui-dialog-from-my-c-sharp-code-behind