Question

I have the following code to register a javascript function on Page_Load (also tried it on Page_Init). The javascript switches two panels from hidden to shown based on a parameter on load of page:

    protected void Page_Load(object sender, EventArgs e)
    {
        String switchAction = "<script language='javascript'>switchactionpanel(" + (int)((Global.upAction)Enum.Parse(typeof(Global.upAction), Global.ProfileAction.ToString())) + ")</script>";
        Page.RegisterClientScriptBlock("switchaction", switchAction);
    }

But when the page loads I am receiving an error: $ is not defined.

I looked in Firebug and the jQuery files are being loaded however, the first file that is being loaded in the .Net tab is the page itself. I know the jquery is correct as the same code works on a different page. Where should my RegisterClientScriptBlock be put in the page lifecycle to work correctly when the page loads? Or am I going about this all wrong?

Était-ce utile?

La solution

You just need to ensure that the inserted script gets inserted after the JQuery reference.

Use RegisterStartupScript instead -- that inserts the script tag before </form> closing tag.

Autres conseils

Not sure if this is relevant but I always use: <script type="text/javascript"...

Sorry this should have been a comment rather than an answer.

I think it has to do with the register function your using. Try using RegisterStartupScript instead of RegisterClientScriptBlock.

Page.ClientScript.RegisterStartupScript(this.GetType(), "switchaction", "<script language='javascript'>switchactionpanel(" + (int)((Global.upAction)Enum.Parse(typeof(Global.upAction), Global.ProfileAction.ToString())) + ")</script>", false);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top