TextBox Tips and Tricks Part-I

SheoNarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 7142 red flag
Rating: 3.67 out of 5  
 3 vote(s)

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
use of it.

TextBox control is used to display a Text box on the web page to take input from the user. 

I have written hundreds of ASP.NET, jQuery, ASP.NET AJAX, HTML, CSS, JavaScript How to Tips and Tricks recently, click here to get them all.


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.


ASPX Page

<asp:TextBox ID="txtFirstName" runat="server" />

Code Behind

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.

ASPX PAGE

<p>Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" /></p>

OUTPUT

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).

ASPX Page

<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)

OUTPUT


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.

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)