- slide 1 of 5
What is an ASP.NET Textbox?
The ASP.NET textbox is the ASP.NET control version of a regular HTML textbox as defined by the <input> tag with a type of "text." Generally you can perform the same functions with either the HTML or ASP.NET textbox, but the ASP.NET version has additional customization and ASP.NET specific properties such as theming. The ASP.NET textbox also integrates with the Ajax Control Toolkit, allowing it to be extended in various ways to improve the user experience.
- slide 2 of 5
How to Use an ASP.NET Textbox?
The best way to learn any type of programming is by example, so let's jump right in. We are going to build a page with a form containing an ASP.NET textbox for the user to enter their name, a button to submit that name, and a blank label. Then, we will demonstrate how to use the submitted value from the textbox to populate that label. The code for our page is as follows:
<form id="form1" runat="server">
What is your name? <br />
<asp:TextBox ID="txtMyName" runat="server" /> <br />
<asp:Button ID="btnChangeName" Text="Submit Name" PostBackURL="" runat="server" /><br />
<asp:Label runat="server" ID="lblMyName" />
Which will render like this:
We now have a form composed of an ASP.NET textbox and a button. When the button is clicked, it will post the values of each control in the form to the page defined by the PostBackURL property. The only active control in our form in this case is the ASP.NET textbox, and since the postback URL is blank it will post to the current page. Therefore we are submitting the text within the textbox back to the current page and reloading it. If you wanted to use the data from the form in a different page, you could change the PostBackURL property to point at that page.
- slide 3 of 5
Using the Data From an ASP.NET Textbox
The next step is to do something with the data submitted by the form we just created. We set the PostBack URL to be the same as the page containing the form, so we will use the data to populate the blank label. In the Page_Load section of our codebehind file we would include the following code:
//Check if the page is a postback (form submission)
//We only have one form so we know which controls will be included
//Set the label text to include the submitted Textbox text
lblMyName.Text = "Your name is " + Request.Form["txtMyName"].ToString();
This very simple code snippet first checks if the page is a postback, which in our example we know means that the form has been submitted. The label text is then set to a string which includes the submitted text from the textbox. When I enter my name and click submit name, the returned page will look like this:
- slide 4 of 5
Extending the ASP.NET Textbox
The ASP.NET Ajax Control Toolkit provides several very useful ways to extend the capability of an ASP.NET textbox through the following controls:
- The Masked Edit extender attaches a "mask" to an ASP.NET textbox, limiting the input to either a number, a date, a time, or a date and time.
- The Resizable Control extender allows the user to dynamically resize a textbox on the page.
- The TextBox Watermark extender places a watermark over a textbox until the user selects it, allowing you to provide input guidance without cluttering the page.
- The ASP.NET AutoComplete extender provides input suggestions based upon what a user has entered into a textbox by calling a web service or page method.