Nana
Nana

Reputation: 31

Include jQuery into C#

How to include jQuery library (.js) into webpart on С#? I need to write script, which using jQuery, and use it for action on click.

p.s.: Webpart is not visual

p.s.: is it real? =)

Upvotes: 0

Views: 1987

Answers (1)

Rawling
Rawling

Reputation: 50114

Basically, you need to include a line like

Page.ClientScript.RegisterClientScriptInclude(
    "JQueryInclude",
    "/path/to/your/jquery.js");

somewhere in your webpart's code where it will be called on every page load, e.g. in the OnLoad method:

protected override void OnLoad(EventArgs e)
{
    Page.ClientScript.RegisterClientScriptInclude(
        "JQueryInclude",
        "/path/to/your/jquery.js");
    base.OnLoad(e);
}

This will cause a line like

<script src="/path/to/your/jquery.js" type="text/javascript"></script>

to appear in your page source, and you can then use JQuery in your webpart.

Upvotes: 4

Related Questions