| Understanding the TextBox ASP.NET control | ||||||||||||||||||||||||
| Taking a quick look at the TextBox ASP.NET web server control - the only control designed for user input. TextBox has several faces: single-line text entry, password entry or multi-line text entry. | ||||||||||||||||||||||||
Welcome to the fourteenth chapter of the FREE online programming course: In the Introduction to Web Server Controls chapter, we've divided ASP.NET Web Server controls into several categories. Our discussion on ASP.NET web server controls started with an overview of postback raising controls: buttons. We continue our exploration of the System.Web.UI.WebControls namespace with the only control designed for user input: TextBox. Enabling text entry on a Web Form pageThe TextBox Web server control provides a way for users to type information into a Web Forms page, including text, numbers, and dates.This "all mighty" control replaces the distinct Text and Password type <input> controls as well as the TextArea control used by HTML. The properties of the TextBox control enable you to configure the control to perform functions equal to those of HTML elements it replaces. When you drop a TextBox control on a Web Form, the generated HTML in the "aspx" view will look like:
By default, the TextBox control renders as a single-line entry box. The following properties determine the actual rendering and functionality of the TextBox control:
AutoPostback from TextBox (or other Input Controls) The TextChanged eventThe most important event for the TextBox control is TextChanged. When a Web Form is submitted, and the data in the TextBox control has changed, the TextChanged event will be fired and processed on the server. ASP.NET uses the ViewState to determine whether the data in the TextBox has changed from the last postback.Note that the Web Form can be "submitted" either if the user has clicked any of the button-like web controls, or the AutoPostback property of the TextBox has been set to true, the content of the control has changed and the user has left the TextBox (the input focus shifted away from the control to another). We will devote an entire chapter to the ViewState and event processing later in this Course. "Login" exampleLet's now put the TextBox control in action. Having a Web Form opened in the "design" view, drop two TextBox controls on it. Leave the default value of the TextMode (SingleLine) for the first TextBox, set the TextBoxMode.MultiLine for the TextMode property of the second TextBox. Also set the Rows property to 5, for example. Set the ID property of the first TextBox to "txtName", assign "txtMemo" for the second TextBox's ID property. Set the AutoPostback property for the "txtName" TextBox to true.Assign the following code for the TextChanged event for the first TextBox (txtName):
Run the project, once the Web Form is displyed in the browser, add some text in the first text box. Hit the Tab key (to shift the focus from this TextBox to another control). Since the AutoPostback property is set to True, the page will be submitted to the server, the TextChanged event will be processed and the data entered in the txtName TextBox will be added to the txtMemo TextBox. Since we have just started exploring web control, we are not yet "qualified" for more complex examples. Being the only data entry control, we'll be using the TextBox in almost every of the exampled in the coming chapters. Only numbers? Only Date values?Suppose you want to allow only numbers to be entered in the TextBox. If you are familiar with Delphi's TEdit control for Win 32 development, you might be tempted to handle the OnKeyPress event of the control to disable non-numeric characters to be entered in the TextBox. Another component that comes to my mind is the TUpDown control (allows users to change the size of a numerical value by clicking on arrow buttons). Since the ASP.NET's TextBox does NOT provide a simple mechanism to handle the OnKeyPress - type of event (after all, this should be handled on the server), you would need to either add some JavaScript code manually, or reach for a third-party implementation.To the next chapter: A Beginner's Guide to ASP.NET Programming for Delphi developersThat's it for today, in the next chapter we'll continue to explore Web Controls. Stay tuned!If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts. A Beginner's Guide to ASP.NET Programming for Delphi developers: Next Chapter >> |
||||||||||||||||||||||||
