How to submit a page to the server when the TextBox value has changed.
<asp:textbox id="TextBox1" runat="server" autopostback="true" ontextchanged="TextBoxValueChanged" />
protected void TextBoxValueChanged(object sender, EventArgs e)
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.
<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
<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.
<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.
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.
<asp:TextBox ID="txtBoxMultiLine" runat="server" TextMode="MultiLine" Rows="5" Columns="50">This is the text.</asp:TextBox>
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 !