New to Telerik UI for ASP.NET Core? Download free 30-day trial

Getting Started with the Barcode

This tutorial explains how to set up a basic Telerik UI for ASP.NET Core Barcode and highlights the major steps in the configuration of the component.

You will initialize a Barcode control and configure the encoding type, value and appearance. Finally, you can run the sample code in Telerik REPL and continue exploring the components.

Sample Telerik UI for ASP.NET Core Barcode

Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:

  • You can use the Telerik REPL playground and skip installing the components on your system and configuring a project.

  • You can prepare a Visual Studio project by following either of these guides:

1. Prepare the CSHTML File

The first step is to add the required directives at the top of the .cshtml document:

  • To use the Telerik UI for ASP.NET Core HtmlHelpers:

    @using Kendo.Mvc.UI
    
  • To use the Telerik UI for ASP.NET Core TagHelpers:

    @addTagHelper *, Kendo.Mvc
    

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and apply some basic styles.

2. Initialize the Barcode

Use the Barcode HtmlHelper or TagHelper to add the component to a page:

  • The Name() configuration method is mandatory as its value is used for the id and the name attributes of the Barcode element.

  • The Value() configuration method sets the value of the Component.

  • The Encoding() configuration property determines the symbology used by the Component.

@using Kendo.Mvc.UI

@(Html.Kendo().Barcode()
    .Name("barcode")
    .Value("10110110")
    .Encoding(BarcodeSymbology.Code128)
)
@addTagHelper *, Kendo.Mvc

<kendo-barcode name="barcode" value="10110110" type="BarcodeSymbology.Code128">
</kendo-barcode>

3. Configure the Appearance of the Barcode

The configuration methods Width, Height, Color, Border, etc. allow you to control the appearance of the Barcode Component.

@using Kendo.Mvc.UI

@(Html.Kendo().Barcode()
    .Name("barcode")
    .Value("10110110")
    .Encoding(BarcodeSymbology.Code128)
    .Width(480)
    .Height(100)
    .Border(border => border.Color("red").Width(1))
)

@addTagHelper *, Kendo.Mvc

<kendo-barcode name="barcode" value="10110110" width="480" height="100" type="BarcodeSymbology.Code128">
    <border color="red" width="1"/>
</kendo-barcode>

4. (Optional) Reference Existing Barcode Instances

To use the client-side API of the Barcode and build on top of its initial configuration, you need a reference to the Barcode instance. Once you get a valid reference, you can call the respective API methods:

  1. Use the .Name() (id attribute) of the component instance to get a reference.

        <script>
            var barcodeReference = $("#barcode").data("kendoBarcode"); // barcodeReference is a reference to the existing instance of the helper.
        </script>
    
  2. Use the client-side API of the Barcode to control the behavior of the widget. In this example, you will use the redraw method to disable the Barcode.

        <script>
            $(document).ready(function () {
                var barcode = $("#barcode").data("kendoBarcode");
    
                barcode.redraw();
            })
        </script>
    

    For more examples, refer to the Demo of the Barcode client API.

Explore this Tutorial in REPL

You can continue experimenting with the code sample above by running it in the Telerik REPL server playground:

Next Steps

See Also

In this article