TextBox Tips and Tricks Part-II

SheoNarayan
Posted by in ASP.NET category on for Beginner level | Points: 250 | Views : 6150 red flag

In this article we shall learn how to submit a page to the server when the TextBox value has changed, specify the width of the TextBox, specify the Maximum characters allowed for the TextBox, making the textbox readonly or disabled, and displaying multi-line textbox in asp.net.

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

I have written hundreds of .NET How to Tips and Tricks recently, click here to get them all.

In this article we shall continue to learn Tips and Tricks of TextBox from my previous article. If you have missed the first part of the TextBox Tips and Tricks article, click here.

How to submit a page to the server when the TextBox value has changed.

ASPX PAGE

<asp:textbox id="TextBox1" runat="server" autopostback="true" ontextchanged="TextBoxValueChanged" />


 CODE BEHIND

 

protected void TextBoxValueChanged(object sender, EventArgs e)

{

Response.Write(TextBox1.Text.Trim());

}

Specify AutoPostBack property of the TextBox to true that will submit the page to the server. If we want to specify our own event that should fire when the TextBox value is changing then we can attach a server side method to the OnTextChanged event. For example in the above code snippet, I have handled ontextchange event and attached TextBoxValueChanged method that fires when the TextBox value is changed by the user. This method writes Text Box value on the web page.

How to specify the width of the TextBox.

In case we need to specify the width of the TextBox so that user will have more visible space into the TextBox to enter the data, we can use this approach.

ASPX PAGE

<asp:TextBox ID="txtBox1" runat="server" Columns="80" />

<asp:TextBox ID="TextBox2" runat="server" Width="50" Columns="80" />

To set the the display width of the TextBox in characters, set the Columns property; to set the width in terms of pixel for the TextBox use Width property. In case we set both, the Width property takes the precedence.

How to specify the Maximum characters allowed for the TextBox

ASPX PAGE

<asp:TextBox ID="TextBox3" runat="server" MaxLength="5" />

To specify the Maximum characters allowed in the TextBox, we can set the MaxLength property. This property is valid only for the TextBox and Password box.

How to make the TextBox readonly or disabled.

In case we want to make the text box read only so that user will not be able to modify the data, we can use this approach. Also if we want to disable a textbox so that user will not be able to even place his cursor to the TextBox we can use this approach.

ASPX PAGE

<asp:TextBox ID="txtReadOnly" runat="server" ReadOnly="true" Text="ReadOnly" />

To make the TextBox readonly, ReadOnly property can be set to “true”. Making the TextBox readonly enables the user to place the cursor on the TextBox but he/she will not be able to edit the TextBox value.

<asp:TextBox ID="TextBox4" runat="server" Enabled="false" Text="Disabled" /> 

To disable the TextBox, Enabled property can be set as false. Making the TextBox disabled stops the user to even place the cursor on the TextBox and disables the TextBox.

OUTPUT


How to display a multi-line textbox and specify the height and width of the textbox.

In case we need to provide a textbox to the user where he/she can enter long content having many lines or paragraphs and want to specifiy the width and height of the textbox (it is called textarea in html), we can use this approach.

ASPX PAGE

 <asp:TextBox ID="txtBoxMultiLine" runat="server" TextMode="MultiLine" Rows="5" Columns="50">This is the text.</asp:TextBox>

OUTPUT

To display a multi-line TextBox, set the TextBox’s TextMode property as Multiline and set the rows and columns properties to set the height and width of the TextBox respectively. Setting rows as 5 will allow the TextBox to display 5 lines of the content in the TextBox and after that a vertical scrollbar appears.

 

Please note that MaxLength property doesn’t work for the TextBox when the TextMode is Multiline.


Thanks for reading and stay tuned for more 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)