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!

5
  • 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. Commented Oct 3, 2012 at 8:05
  • Do you get any errors? Can you see your show(); code rendered on the page after the button click? Commented Oct 3, 2012 at 8:05
  • Please have a look : stackoverflow.com/questions/5462360/… Commented Oct 3, 2012 at 8: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; } Commented Oct 3, 2012 at 14:22
  • now it can works well! thanks Murali, Lazarus, Tim B James and huMpty duMpty Commented Oct 3, 2012 at 14:25

1 Answer 1

1

Try use ScriptManager.RegisterStartupScript method

See Difference between RegisterStartupScript and RegisterClientScriptBlock?

Sign up to request clarification or add additional context in comments.

3 Comments

thanks! I tried ScriptManager.RegisterStartupScript but it show modal and close quickly ,jusk spalish once! any method can i try?
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
my code block containing 'show()' has executed before the page has fininsh loading! thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.