Getting Started with the TextArea
This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC TextArea and highlights the major steps in the configuration of the component.
You will initialize the TextArea component, customize it, and configure its label functionality. Finally, you will learn how to handle the events of the TextArea.
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, and paragraphs.
@using Kendo.Mvc.UI
<h4>TextArea with a placeholder</h4>
<div>
</div>
2. Initialize the TextArea
Use the TextArea HtmlHelper to add the component to a page:
- The
Name()
configuration method is mandatory as its value is used for theid
and the name attributes of the TextArea element. - The
Rows()
configuration method lets you specify the number of visible text lines for the component.
@(Html.Kendo().TextArea()
.Name("textArea")
.Rows(5)
)
3. Configure the Label
The next step is to enable the label functionality for the TextArea. The following example will configure the floating label functionality and specify the label's content.
@(Html.Kendo().TextArea()
.Name("textArea")
.Rows(5)
.Label(label => {
label.Content("Description");
label.Floating(true);
})
)
4. Customize the TextArea
To change the appearance of the TextArea, use any of its built-in styling options, for example, Size()
and FillMode()
.
@(Html.Kendo().TextArea()
.Name("textArea")
.Rows(5)
.Size(ComponentSize.Medium)
.FillMode(FillMode.Outline)
.Label(label => {
label.Content("Description");
label.Floating(true);
})
)
5. Handle the TextArea Events
The TextArea exposes various events that you can handle and further customize the functionality of the component. In this tutorial, you will use the Change
event to display a popup message when the value of the TextArea changes through user interaction.
@(Html.Kendo().TextArea()
.Name("textArea")
.Rows(5)
.Size(ComponentSize.Medium)
.FillMode(FillMode.Outline)
.Events(events => events.Change("onChange"))
.Label(label => {
label.Content("Description");
label.Floating(true);
})
)
<script>
function onChange(e){
alert("Changed value: "+ e.sender.value());
}
</script>
6. (Optional) Reference Existing TextArea Instances
You can reference the TextArea instances that you have created and build on top of their existing configuration:
-
Use the
.Name()
(id attribute) of the component instance to get a reference.<script> $(document).ready(function() { var textAreaReference = $("#textArea").data("kendoTextArea"); // textAreaReference is a reference to the existing TextArea instance of the helper. }) </script>
-
Use the TextArea Client-Side API to control the behavior of the widget. In this example, you will change the value of the widget programmatically.
<script> $(document).ready(function() { var textAreaReference = $("#textArea").data("kendoTextArea"); textAreaReference.value("new value"); textAreaReference.trigger("change"); // Trigger the change event of the widget in order to ensure that the updated value is processed. }) </script>