3

This is a problem i've tried so solve before but gived up. Basicly i'm using ModalPopupExtenders (from AJAX.NET) to display Panels with a few content (text, controls, etc). And i'm calling it from codebehind. And it works well.

But now i want to replace the ModalPopup with some jQuery dialog box. The problem is calling it from codebehind. As far as i know, i have to register the jQuery libraries on RegisterStartup event, but i've tried it and call the jQuery from codebehind but with no sucess.

Can someone help me? I really want to replace the ModalPopup, they gives me to much trouble.

Thanks in advance.


protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "registerDialog",
        "$(function() { $('#dialog').dialog({autoOpen:false, show:'blind'}); });", true);
}

protected void Button1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "openDialog",
        "$('#dialog').dialog('open');", true);
}

Is this the correct way? I've to register it first to stay hidden. Thanksю

2 Answers 2

4

If you're using a ScriptManager, use RegisterStartupScript(), like this:

ScriptManager.RegisterStartupScript(this, GetType(), "modalscript",
    "$(function() { $('#dialog').dialog(); });", true);

If you're not using a ScriptManager/UpdatePanels, use the equivalent ClientScriptManager version.

It's important to remember to wrap your code in a document.ready handler (IE has the most issues without it), so your elements (in my example, id="dialog") are in the DOM and ready.

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

2 Comments

Thank you Nick. When you say to wrap my code in a document.ready, in the sample i've showed bellow i did all from ScriptManager.RegisterStartupScript Is there anything i should change? I've though in write the registerDialog on document.ready, but i don't know if it's the same.
@Guilherme - Change the this (first argument) to the UpdatePanel you're in if that's the case, also you're not using the $(function() { }); wrapper in your RegisterStartupScript call, make sure to do this :)
1

You do not actually call jQuery from code behind, you just write some extra javascript code that's run on page load (after the PostBack).

In this start up code you make the jQuery calls.

Comments

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.