# Call Jquery Code From Code Behind

**URL:** <https://discuss.workflowgen.com/t/call-jquery-code-from-code-behind/1406>\
**Category:** Development\
**Created:** [June 17, 2019, 9:03am UTC](https://discuss.workflowgen.com/t/call-jquery-code-from-code-behind/1406 "2019-06-17T09:03:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sara](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@sara](https://discuss.workflowgen.com/u/sara)\
**Post date:** [June 17, 2019, 9:03am UTC](https://discuss.workflowgen.com/t/call-jquery-code-from-code-behind/1406/1 "2019-06-17T09:03:04Z")

</div>

Hi,

I need to call a Jquery function from CodeBehind, I know that I should use Page.ClientScript.RegisterStartupScript() function but I don’t know how .

If you have an example of how to use it will be helpful.

Best Regards,  
Sara.

---

<div class="post-metadata">

**Author:** ![jianan.hsu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discuss.workflowgen.com/jianan.hsu/32/411_2.png) [@jianan.hsu](https://discuss.workflowgen.com/u/jianan.hsu)\
**Post date:** [June 18, 2019, 10:32pm UTC](https://discuss.workflowgen.com/t/call-jquery-code-from-code-behind/1406/2 "2019-06-18T22:32:07Z")

</div>

Hi,

jQuery functions are usually run at client-side. You can simply embed your code in the `Web References` tab in the Form Designer.

Here are some articles on how you can include and execute client-side JavaScript/jQuery scripts.

> [@How to create forms with collapsible and expandable sections](https://discuss.workflowgen.com/t/how-to-create-forms-with-collapsible-and-expandable-sections/301):
>
> You can make form sections collapsible and expandable using JavaScript and the FORM\_FIELDS\_COLLAPSED parameter. In the Form Designer, add a section called FORM at the top of the form. The code below will set it to hidden for every web form action. Add a textbox field with the ID FIELDS\_COLLAPSED to the FORM section. On the Web References tab in the Form Designer configuration panel, check the Include jQuery API and jQuery UI libraries, and add the following code: \<script type="text/jav…

> [@How to prevent forms from being submitted twice](https://discuss.workflowgen.com/t/how-to-prevent-forms-from-being-submitted-twice/295):
>
> To prevent forms from being submitted twice, such as when a user double-clicks on the Submit button, add the following JavaScript code, including the opening and closing tags, on the Web References tab in the Form configuration panel. This is supported in Internet Explorer 9 and later, Edge, Chrome, and Firefox. // if $selectedObject is undefined, we are not in the form designer if (typeof $selectedObject === "undefined") { var windowFirstLoad=true; // Standard pag…

> [@Form Designer: Add JavaScript functions](https://discuss.workflowgen.com/t/form-designer-add-javascript-functions/269):
>
> To add JavaScript functions to your web form, open the Form configuration panel and click the Web References tab. Add tags and place your JavaScript code inside. Note: The JavaScript code is activated in the form designer, so it can impact the form designer behavior.

> [@Form Designer: How to ensure custom client-side JavaScript code is executed at runtime only](https://discuss.workflowgen.com/t/form-designer-how-to-ensure-custom-client-side-javascript-code-is-executed-at-runtime-only/294):
>
> To ensure that your custom client-side JavaScript code is executed in runtime mode only, enclose your JavaScript code with the following IF block: if (typeof $selectedObject === "undefined") { // Place your code here }

---

<div class="post-metadata">

**Author:** ![sara](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@sara](https://discuss.workflowgen.com/u/sara)\
**Post date:** [June 19, 2019, 6:03am UTC](https://discuss.workflowgen.com/t/call-jquery-code-from-code-behind/1406/3 "2019-06-19T06:03:39Z")

</div>

Hi,

Thank you for the reply. I have already used Jquery to do some stuff in client side but I need to call a specific Jquery function from Code Behind .

Best Regards,

---

<div class="post-metadata">

**Author:** ![jianan.hsu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discuss.workflowgen.com/jianan.hsu/32/411_2.png) [@jianan.hsu](https://discuss.workflowgen.com/u/jianan.hsu)\
**Post date:** [June 19, 2019, 2:02pm UTC](https://discuss.workflowgen.com/t/call-jquery-code-from-code-behind/1406/4 "2019-06-19T14:02:26Z")

</div>

Hi,

The `ClientScriptManager.RegisterStartupScript` method allows you to inject at client JS when the page postback. Is that what you would like to achieve?

> **[ClientScriptManager.RegisterStartupScript Method (System.Web.UI)](https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.clientscriptmanager.registerstartupscript?view=netframework-4.8)**
>
> Registers the startup script with the object.

Here is a sample process of how it is being used in the form designer. You can open the Form / .NET tabs to view the code behind I have added.

[https://advantys.box.com/s/m5t46uv0sfh27bjobxduwshms6f7ipdh](https://advantys.box.com/s/m5t46uv0sfh27bjobxduwshms6f7ipdh)

#### Form / .NET / Code Behind

```auto
	// Place here your ASP.NET procedures and global variables.
	// It will be declared within a <script runat="server"> block,
	// and placed before the opening <html>.
	// Do not include the <script runat="server"> tag.
	// Use the Control key + Spacebar to show the keywords list. 
	// ***********************************************************

	protected void Page_Load(object sender, EventArgs e)
	{
	    base.Page_Load(sender, e);
	    
	    // Define the name and type of the client scripts on the page.
        String csname1 = "PopupScript1";
        Type cstype = this.GetType();
            
        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;
    
        // Check to see if the startup script is already registered.
        if (!cs.IsStartupScriptRegistered(cstype, csname1))
        {
            StringBuilder cstext1 = new StringBuilder();
            cstext1.Append("<script type=text/javascript> alert('JS injected at Page_Load') </");
            cstext1.Append("script>");
    
            cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
        }
	}

    protected void REQUEST_BUTTON1_Click(object sender, EventArgs e) 
    {
        // Define the name and type of the client scripts on the page.
        String csname1 = "PopupScript2";
        Type cstype = this.GetType();
            
        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;
    
        // Check to see if the startup script is already registered.
        if (!cs.IsStartupScriptRegistered(cstype, csname1))
        {
            StringBuilder cstext1 = new StringBuilder();
            cstext1.Append("<script type=text/javascript> alert('JS injected on custom button click event') </");
            cstext1.Append("script>");
    
            cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
        }
    }

```

---

<div class="post-metadata">

**Author:** ![jianan.hsu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discuss.workflowgen.com/jianan.hsu/32/411_2.png) [@jianan.hsu](https://discuss.workflowgen.com/u/jianan.hsu)\
**Post date:** [June 19, 2019, 2:16pm UTC](https://discuss.workflowgen.com/t/call-jquery-code-from-code-behind/1406/5 "2019-06-19T14:16:40Z")

</div>

Here is another sample process that would call an existing JS function that was defined in the `Web References` tab in the Form designer on click of a button.

[https://advantys.box.com/s/qllouhovadg7dlyecn32ccvfxi07fc8q](https://advantys.box.com/s/qllouhovadg7dlyecn32ccvfxi07fc8q)

---

<div class="post-metadata">

**Author:** ![sara](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@sara](https://discuss.workflowgen.com/u/sara)\
**Post date:** [June 20, 2019, 6:07am UTC](https://discuss.workflowgen.com/t/call-jquery-code-from-code-behind/1406/6 "2019-06-20T06:07:55Z")

</div>

Hi,

Thank you for your helpful reply .

Best Regards.
