Introduction
SharePoint Web parts can modify the content, appearance, and behavior of pages of a SharePoint site, by using a browser. Web parts are server-side controls that run inside a web part page. They are the building blocks of pages that appear on a SharePoint site. In this article, you will learn how to call the JavaScript function from SharePoint Visual WebPart.
Pre-process
- Create or open a SharePoint project.
- Choose the SharePoint project node in Solution Explorer and then choose Project --> Add New Item.
- In the Add New Item dialog box, expand the SharePoint node.
- In the list of SharePoint templates, choose Visual Web Part.
- In the Name box, specify a name for the web part, and click the Add button.
- The web part appears in Solution Explorer.
- In Solution Explorer, open the code file for the web part that you just created.
Calling the JavaScript function from WebPart
The following section explains the flow of calling the JavaScript function from webpart.cs file.
- Create one JavaScript file and add function JavaScriptFunctionName().
- function JavaScriptFunctionName()
- {
- alert('Hello World");
- }
- Give the link of this JavaScript file in webpart.ascx file, as shown below.
- <script src="/Style Library/scripts/TestScript.js" type="text/javascript"></script>
- Place the button control in Webpart.ascx file.
- <button type="button" id="btnSearch" runat="server" nserverclick="btnSearch_ServerClick">Search</button>
- Write the code-behind function in Webpart.cs file.
- protected void btnSearch_ServerClick(object sender, EventArgs e)
- Upon clicking the Search button, the above function btnSearch_ServerClick will be called.
- We need to call the RegisterStartupScript method of Page.ClientScript object inside the above function. In this function, we need to give the parameters mentioned below.
- Type of function as this.GetType().
- Unique keyword for function.
- JavaScript for function call.
- Boolean value to add script tag or not.
- Page.ClientScript.RegisterStartupScript(this.GetType(), "functioncallKey", "<script type='text/javascript' >JavaScriptFunctionName();</script>", false);
- The above code will call the JavaScriptFunctionName() function inside the JS file.
Call JavaScript function from UpdatePannel of WebPart
The above method would not work when the button is placed inside the ASP UpdatePannel. So, we need to change the above function of calling the JavaScript function.
- Place the button control under the UpdatePannel in Webpart.ascx file.
- <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Always">
- <ContentTemplate> <button type="button" id="btnSearch" runat="server" serverclick="btnSearch_ServerClick">Search</button> </ContentTemplate>
- <Triggers>
- <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="serverclick"></asp:AsyncPostBackTrigger>
- </Triggers>
- </asp:UpdatePanel>
- Write the code-behind function in Webpart.cs file.
- protected void btnSearch_ServerClick(object sender, EventArgs e)
- On click of Search button, the above function btnSearch_ServerClick will be called.
- We need to call the RegisterClientScriptBlock method of ScriptManager object inside the above function. In this function, we need to give the parameters, as shown below.
- Control to the function.
- Type of function as this.GetType().
- Unique keyword for function.
- JavaScript for function call.
- Boolean value to add script tag or not.
- ScriptManager.RegisterClientScriptBlock((sender as Control), this.GetType(), "functioncallKey","JavaScriptFunctionName()", true);
- Above code will call the JavaScriptFunctionName() function inside the JS file.
Summary: Thus, you have learned how to call a JavaScript function from SharePoint Visual WebPart.