How do I open a jQuery UI Dialog from my c# code behind?

好久不见. 提交于 2019-12-21 20:42:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!