In this article we shall learn how to programmatically set the TextBox value and get the TextBox value, render a password type textbox and how to specify the AutoComplete type of the TextBox and what is the
TextBox control is used to display a Text box on the web page to take input from the user.
First we shall learn how to programmatically set the TextBox value and get the TextBox value.
In order to demonstrate this I am going to create a simple .aspx page and below is the code for this page.
<asp:TextBox ID="txtFirstName" runat="server" />
protected void Page_Load(object sender, EventArgs e)
// set textbox value
txtFirstName.Text = "Set from code-behind";
// retrieve textbox value
string firstName = txtFirstName.Text;
The ASPX code (if the code behind will not be there) will display a blank Text box on the web page. If
code behind code is written, it sets the the value into the TextBox. The next line retrieves the value of the TextBox into the firstName variable.
Now we shall learn how to render a password type textbox.
In case we need to render a password text box so that the actual text doesn’t appear as user type in the text box, we can use this approach. This is frequently used to enter password or secret code by the user on the web page so that others can’t know what is being written into the TextBox.
<p>Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" /></p>
To render the Password type TextBox, we need to specify the TextMode property of the TextBox as “Password”.
Now let's learn how to specify the AutoComplete type of the TextBox and what is the use of it.
In case we need to provide ability to user to enter previously entered data into the TextBox quickly we can use this feature so that an autocomplete dropdown is displayed against the textbox with all previously entered items for that TextBox. User can select them to enter that data quickly instead of typing it everytime.
AutoComplete type is a kind of intellisense to the TextBox that appears when user try to type characters into the TextBox. This helps in faster data entering to the HTML form. In order to specify a certain category of AutoComplete type that is shared across all the web pages, we can specify AutoCompleteType property of the TextBox. This property is valid only for the TextBox, not for the Password textbox or multiline TextBox (textarea).
<asp:TextBox ID="txtUserName" runat="server" AutoCompleteType="None" />
In order to share all categories of AutoCompleteType to the TextBox, set its value as “None”, if we want to disable the AutoComplete feature of the TextBox, we can set its properties as “Disabled”. If we want a certain type of AutoComplete to display for a particular TextBox, we can select from the enumeration values that appear in the Visual Studio eg. Company, Department etc. (displayed in the picture below)
Hope this information was useful. Keep reading and sharing your knowledge to the world!
Thanks for reading and stay tuned for more Tips and Tricks articles.