Easily Use a JavaScript Control in a Blazor Server App | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (915)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Easily Use a JavaScript Control in a Blazor Server App

Easily Use a JavaScript Control in a Blazor Server App

Blazor is an open-source web development framework. It is used to build interactive single-page web apps using C# instead of JavaScript. It supports components, lifecycle, routing, bindings, validations, templates, dependency injection, error handling, and more features to quickly create real-time apps. Although Blazor avoids using JavaScript when building an application, it still supports using JavaScript controls when needed. To demonstrate this, we are going to include the Syncfusion JavaScript Spreadsheet control in a Blazor application.

Prerequisites

To create and run a Blazor server-side web app, we need the following prerequisites:

Note:

.NET Core SDK 3.1.8 requires Visual Studio 2019 16.7 or later.

.NET 5.0 requires Visual Studio 2019 16.8 or later..

.NET 6.0 requires Visual Studio 2022 17.0 Preview 4.1 or later.

Create a new Blazor server-side project in Visual Studio 2022

Follow these steps to easily create a Blazor server-side app in Visual Studio 2022:

  1. First, open Visual Studio 2022. Then, choose the Create a new project option.
    Refer to the following image.
    Create a new project in Visual Studio 2022
  2. Now, the Create a new project window will appear. Choose the Blazor Server App as the template, and then click Next.
    Create Blazor Server App using Visual Studio 2022
  3. Now, the project configuration window will pop up. Provide a name to the project and click Next.
    Configure Blazor Server App in Visual Studio 2022
  4. The Additional information window will appear. Select your required target framework as .NET Core 3.1.NET 5.0, or .NET 6.0. The Configure for HTTPS checkbox will be enabled by default. Now, click the Create button to create a new Blazor server app. Here, we are going to select .NET 6 as the target framework.
    Adding Additional information to Blazor Server App

We have created our Blazor server-side app. Let’s see how to add the JavaScript Spreadsheet control in it.

Add style and script for the JavaScript Spreadsheet

To integrate the JavaScript Spreadsheet control within our Blazor app, we need to add the required client-side style and scripts through CDN references in the <head> element of the ~/Pages/_Layout.cshtml page.

Refer to the following code.

[_Layout.cshtml]

<head>
    <link href=" https://cdn.syncfusion.com/ej2/19.4.38/bootstrap5.css" rel="stylesheet" />
    <script src="https://cdn.syncfusion.com/ej2/19.4.38/dist/ej2.min.js" type="text/javascript"></script>
</head>

Initialize the JavaScript Spreadsheet control

We can invoke a JavaScript API from .NET and a .NET method from JavaScript. Here, we will invoke the JavaScript API from Blazor by injecting the IJSRuntime service into our Index.razor page.

Then, we will add the Spreadsheet target element and invoke the RenderSpreadsheet function in the OnAfterRenderAsync method in the Index.razor page.

Refer to the following code example.

[Index.razor]

@inject IJSRuntime JSRuntime

<div id="spreadsheet" @ref="spreadsheetElem"></div>

@code{
    ElementReference spreadsheetElem;

    List<Order> orders = new List<Order>
    {
        new Order { OrderID = 10001, OrderDate = "12/16/2021", ShipAddress = "Kirchgasse 6", ShipCity ="Berlin", ShipCountry = "Denmark" },
        new Order { OrderID = 10002, OrderDate = "12/16/2021", ShipAddress = "Avda. Azteca 123", ShipCity ="Madrid", ShipCountry = "Brazil" },
        new Order { OrderID = 10003, OrderDate = "12/17/2021", ShipAddress = "Carrera 52 con Ave. Bolívar #65-98 Llano Largo", ShipCity ="Cholchester", ShipCountry = "Germany" },
        new Order { OrderID = 10004, OrderDate = "12/18/2021", ShipAddress = "Magazinweg 7", ShipCity ="Marseille", ShipCountry = "Austria" },
        new Order { OrderID = 10005, OrderDate = "12/20/2021", ShipAddress = "1029 - 12th Ave. S.", ShipCity ="Tsawassen", ShipCountry = "Switzerland" }
    };

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await JSRuntime.InvokeVoidAsync("RenderSpreadsheet", spreadsheetElem, orders);
    }

    public class Order
    {
        public int? OrderID { get; set; }
        public string OrderDate { get; set; }
        public string ShipAddress { get; set; }
        public string ShipCity { get; set; }
        public string ShipCountry { get; set; }
    }
}

To initialize the Syncfusion JavaScript Spreadsheet control, specify the following code in the <script> element of the ~/Pages/_Layout.cshtml page.

[_Layout.cshtml]

<script>
        function RenderSpreadsheet(spreadsheetElem, data) {
            new ej.spreadsheet.Spreadsheet({
                sheets: [{
                    ranges: [{
                        dataSource: data,
                        showFieldAsHeader: false
                    }]
                }],
                openUrl: 'https://ej2services.syncfusion.com/production/web-services/api/spreadsheet/open',
                saveUrl: 'https://ej2services.syncfusion.com/production/web-services/api/spreadsheet/save',
            }, spreadsheetElem);
        }
</script>

Output

After executing the above code, the JavaScript Spreadsheet control will be rendered in our Blazor server-side app.

JavaScript Spreadsheet Control in Blazor Application
JavaScript Spreadsheet Control in Blazor Application

Note: For more details, refer to the Getting started with JavaScript Spreadsheet control documentation.

Resource

For a detailed look at the code, refer to the complete example on GitHub: Adding a JavaScript Spreadsheet control in a Blazor server app.

Conclusion

Thanks for reading! I hope you now have a better understanding of how to add a JavaScript control in your Blazor server app. Try out the steps provided in this blog post and leave your feedback in the comments section below!

The Syncfusion Blazor suite offers over 70 components that work with both Blazor server-side and client-side (Blazor WebAssembly) projects seamlessly. Use them to build astonishing applications!

For existing customers, the newest version of Essential Studio is available for download from the License and Downloads page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out the available features.

Also, you can contact us through our support forumsupport portal, or feedback portal. As always, we are happy to assist you!

Related blogs

Tags:

Share this post:

Comments (1)

Dear Prabu !

Where can we find a sample: How to open an excel file stored on a server with this component in the Blazor Server application?
Thank you in advance for your help!

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed