|
| 1 | +--- |
| 2 | +title: Getting Started |
| 3 | +page_title: Getting Started |
| 4 | +description: "Make your first steps with the Telerik UI for {{ site.framework }} TextBox component by following a complete step-by-step tutorial." |
| 5 | +slug: aspnetcore_textbox_getting_started |
| 6 | +position: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +# Getting Started with the TextBox |
| 10 | + |
| 11 | +This tutorial explains how to set up a basic Telerik UI for {{ site.framework }} TextBox and highlights the major steps in the configuration of the component. |
| 12 | + |
| 13 | +You will initialize a TextBox component with a placeholder text and a label. {% if site.core %}Finally, you can run the sample code in [Telerik REPL](https://netcorerepl.telerik.com/) and continue exploring the components.{% endif %} |
| 14 | + |
| 15 | +  |
| 16 | + |
| 17 | +@[template](/_contentTemplates/core/getting-started-prerequisites.md#repl-component-gs-prerequisites) |
| 18 | + |
| 19 | +## 1. Prepare the CSHTML File |
| 20 | + |
| 21 | +@[template](/_contentTemplates/core/getting-started-directives.md#gs-adding-directives) |
| 22 | + |
| 23 | +You will also add some sample value that the TextBox will present to the user. Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others. |
| 24 | + |
| 25 | +```HtmlHelper |
| 26 | + @using Kendo.Mvc.UI |
| 27 | +
|
| 28 | + <h4>TextBox with a Placeholder</h4> |
| 29 | + <div> |
| 30 | +
|
| 31 | + </div> |
| 32 | +``` |
| 33 | +{% if site.core %} |
| 34 | +```TagHelper |
| 35 | + @addTagHelper *, Kendo.Mvc |
| 36 | +
|
| 37 | + <h4>TextBox with a Placeholder</h4> |
| 38 | + <div> |
| 39 | +
|
| 40 | + </div> |
| 41 | +``` |
| 42 | +{% endif %} |
| 43 | + |
| 44 | +## 2. Initialize the TextBox |
| 45 | + |
| 46 | +Use the TextBox HtmlHelper {% if site.core %}or TagHelper{% endif %} to add the component to a page: |
| 47 | + |
| 48 | +* The `Name()` configuration method is mandatory as its value is used for the `id` and the name attributes of the TextBox element. |
| 49 | +* The `Placeholder()` configuration specifies the dummy text that is shown initially to hint the user. |
| 50 | + |
| 51 | +```HtmlHelper |
| 52 | +@using Kendo.Mvc.UI |
| 53 | +
|
| 54 | +<h4>TextBox with a Placeholder</h4> |
| 55 | +<div> |
| 56 | + @(Html.Kendo().TextBox() |
| 57 | + .Name("textbox") |
| 58 | + .Placeholder("Name...") |
| 59 | + .HtmlAttributes(new { style = "width: 300px;" }) |
| 60 | + ) |
| 61 | +</div> |
| 62 | +``` |
| 63 | +{% if site.core %} |
| 64 | +```TagHelper |
| 65 | +@addTagHelper *, Kendo.Mvc |
| 66 | +
|
| 67 | +<h4>TextBox with a Placeholder</h4> |
| 68 | +<div> |
| 69 | + <kendo-textbox name="textbox" style="width: 300px;" |
| 70 | + placeholder="Name..."> |
| 71 | + </kendo-textbox> |
| 72 | +</div> |
| 73 | +``` |
| 74 | +{% endif %} |
| 75 | + |
| 76 | +## 3. Define a Label Text |
| 77 | + |
| 78 | +The next step is to present some description text in front of the TextBox component using the `Label()` property. |
| 79 | + |
| 80 | +```HtmlHelper |
| 81 | +@using Kendo.Mvc.UI |
| 82 | +
|
| 83 | +<h4>TextBox with a Placeholder</h4> |
| 84 | +<div> |
| 85 | + @(Html.Kendo().TextBox() |
| 86 | + .Name("textbox") |
| 87 | + .Label(l => l.Content("Set value:")) |
| 88 | + .Placeholder("Name...") |
| 89 | + .HtmlAttributes(new { style = "width: 300px;" }) |
| 90 | + ) |
| 91 | +</div> |
| 92 | +``` |
| 93 | +{% if site.core %} |
| 94 | +```TagHelper |
| 95 | + @addTagHelper *, Kendo.Mvc |
| 96 | +
|
| 97 | +<h4>TextBox with a Placeholder</h4> |
| 98 | +<div> |
| 99 | + <kendo-textbox name="textbox" style="width: 300px;" placeholder="Name..."> |
| 100 | + <textbox-label content="Set value:"/> |
| 101 | + </kendo-textbox> |
| 102 | +</div> |
| 103 | +``` |
| 104 | +{% endif %} |
| 105 | + |
| 106 | +## 4. Handle a TextBox Event |
| 107 | + |
| 108 | +The TextBox component provides convenient events for implementing your desired logic. In this tutorial, you will use the exposed `Change()` event to log a new entry in the browser's console. |
| 109 | + |
| 110 | +```HtmlHelper |
| 111 | +@using Kendo.Mvc.UI |
| 112 | +
|
| 113 | +<h4>TextBox with a Placeholder</h4> |
| 114 | +<div> |
| 115 | + <script> |
| 116 | + function change(e) { |
| 117 | + console.log("Change :: " + this.value()); |
| 118 | + } |
| 119 | + </script> |
| 120 | +
|
| 121 | + @(Html.Kendo().TextBox() |
| 122 | + .Name("textbox") |
| 123 | + .Label(l => l.Content("Set value:")) |
| 124 | + .Placeholder("Name...") |
| 125 | + .Events(e => e.Change("change")) |
| 126 | + .HtmlAttributes(new { style = "width: 300px;" }) |
| 127 | + ) |
| 128 | +</div> |
| 129 | +``` |
| 130 | +{% if site.core %} |
| 131 | +```TagHelper |
| 132 | +@addTagHelper *, Kendo.Mvc |
| 133 | +
|
| 134 | +<h4>TextBox with a Placeholder</h4> |
| 135 | +<div> |
| 136 | + <script> |
| 137 | + function change(e) { |
| 138 | + console.log("Change :: " + this.value()); |
| 139 | + } |
| 140 | + </script> |
| 141 | +
|
| 142 | + <kendo-textbox name="textbox" style="width: 300px;" |
| 143 | + placeholder="Name..." on-change="change"> |
| 144 | + <textbox-label content="Set value:"/> |
| 145 | + </kendo-textbox> |
| 146 | +</div> |
| 147 | +``` |
| 148 | +{% endif %} |
| 149 | + |
| 150 | +## 5. (Optional) Reference Existing TextBox Instances |
| 151 | + |
| 152 | +You can reference the TextBox instances that you have created and build on top of their existing configuration: |
| 153 | + |
| 154 | +1. Use the `id` attribute of the component instance to establish a reference. |
| 155 | + |
| 156 | + ```script |
| 157 | + <script> |
| 158 | + var textboxReference = $("#textbox").data("kendoTextBox"); // textboxReference is a reference to the existing TextBox instance of the helper. |
| 159 | + </script> |
| 160 | + ``` |
| 161 | +
|
| 162 | +1. Use the [TextBox client-side API](https://docs.telerik.com/kendo-ui/api/javascript/ui/textbox#methods) to control the behavior of the widget. In this example, you will use the `value` method to select an item. |
| 163 | +
|
| 164 | + ```script |
| 165 | + <script> |
| 166 | + var textboxReference = $("#textbox").data("kendoTextBox"); // textboxReference is a reference to the existing TextBox instance of the helper. |
| 167 | + textboxReference.value("Sample text"); |
| 168 | + </script> |
| 169 | + ``` |
| 170 | +
|
| 171 | +{% if site.core %} |
| 172 | +## Explore this Tutorial in REPL |
| 173 | +
|
| 174 | +You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: |
| 175 | +
|
| 176 | +* [Sample code with the TextBox HtmlHelper](https://netcorerepl.telerik.com/QHEvbJFL14nUQBCX42) |
| 177 | +* [Sample code with the TextBox TagHelper](https://netcorerepl.telerik.com/GnObbJFh14nVCZns54) |
| 178 | +
|
| 179 | +{% endif %} |
| 180 | +
|
| 181 | +## Next Steps |
| 182 | +
|
| 183 | +* [Set Labels to the TextBox]({% slug htmlhelpers_labels_textbox %}) |
| 184 | +* [Customize the Appearance of the TextBox]({% slug textbox_appearance %}) |
| 185 | +* [Accessibility]({% slug accessibility_textbox_aspnetcore %}) |
| 186 | +
|
| 187 | +## See Also |
| 188 | +
|
| 189 | +* [Using the API of the TextBox for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/textbox/api) |
| 190 | +* [TextBox Client-Side API](https://docs.telerik.com/kendo-ui/api/javascript/ui/textbox) |
| 191 | +* [Server-Side API of the TextBox](/api/textbox) |
| 192 | +* [Knowledge Base Section](/knowledge-base) |
0 commit comments