4

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!

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117
wang gang
  • 41
  • 1
  • 3
  • I wonder if your script block containing your 'show()' is getting executed before the page has finished loading. Look at the HTML after your button click and step through the script. – Lazarus Oct 03 '12 at 08:05
  • Do you get any errors? Can you see your `show();` code rendered on the page after the button click? – Tim B James Oct 03 '12 at 08:05
  • Please have a look : **http://stackoverflow.com/questions/5462360/open-jquery-dialog-from-codebehind** – huMpty duMpty Oct 03 '12 at 08:06
  • yes my code block containing 'show()' has some problem! just like Lazarus said,show() get executed before the page has finished loading.I modify it as below function show() { $(function () { $("#dialog_info").dialog("open"); }); return false; } – wang gang Oct 03 '12 at 14:22
  • now it can works well! thanks Murali, Lazarus, Tim B James and huMpty duMpty – wang gang Oct 03 '12 at 14:25

1 Answers1

1

Try use ScriptManager.RegisterStartupScript method

See Difference between RegisterStartupScript and RegisterClientScriptBlock?

Community
  • 1
  • 1
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
  • thanks! I tried ScriptManager.RegisterStartupScript but it show modal and close quickly ,jusk spalish once! any method can i try? – wang gang Oct 03 '12 at 09:09
  • There might be some other function is getting executed after dialog open. Can you check where the show() method is rendered in your code? it should be at end of the page. see view source or try with DOM helper like Firebug for firefox, etc – Murali Murugesan Oct 03 '12 at 09:16
  • my code block containing 'show()' has executed before the page has fininsh loading! thank you! – wang gang Oct 03 '12 at 14:28