Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (155).NET Core  (27).NET MAUI  (10)Angular  (51)ASP.NET  (48)ASP.NET Core  (68)ASP.NET MVC  (88)Azure  (29)Blazor  (114)DocIO  (21)Essential JS 2  (73)Essential Studio  (165)File Formats  (22)Flutter  (117)JavaScript  (153)Microsoft  (104)PDF  (59)React  (35)Succinctly series  (124)Syncfusion  (504)TypeScript  (30)Uno Platform  (3)UWP  (4)Vue  (28)Webinar  (27)Windows Forms  (57)WinUI  (32)WPF  (127)Xamarin  (134)XlsIO  (24)Other CategoriesBarcode  (4)BI  (29)Bold BI  (4)Build conference  (6)Business intelligence  (53)Button  (4)C#  (111)Chart  (54)Cloud  (10)Company  (445)Dashboard  (6)Data Science  (3)Data Validation  (5)DataGrid  (51)Development  (273)Doc  (7)DockingManager  (1)eBook  (94)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (21)Extensions  (16)File Manager  (4)Gantt  (9)Gauge  (8)Git  (4)Grid  (27)HTML  (9)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (245)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (26)Performance  (7)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (6)Road Map  (9)Scheduler  (29)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (19)Solution Services  (2)Spreadsheet  (11)SQL  (7)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (11)Tips and Tricks  (71)UI  (147)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (1)Visual Studio  (22)Visual Studio Code  (13)Web  (237)What's new  (158)Windows 8  (19)Windows App  (1)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
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:

Share on twitter
Share on facebook
Share on linkedin
Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top