Getting Started with the Barcode
This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC 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
.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:
To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can use a project template.
To manually configure an existing project by using NuGet, see the Adding Telerik UI through NuGet.
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 MVC HtmlHelpers:
@using Kendo.Mvc.UI
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 to add the component to a page:
The
Name()
configuration method is mandatory as its value is used for theid
and thename
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)
)
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))
)
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:
-
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>
-
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.