February 23, 2009

The following shows how to apply properties to a Asp.Net TextBox web server control programmatically.

The id of the textbox control is "TextBox1".
TextBox1.AccessKey = "k"; //set accesskey
TextBox1.AutoPostBack = true; //set if autoPostBack is true

TextBox1.BackColor = System.Drawing.Color.Yellow; //set background color
TextBox1.BorderColor = System.Drawing.Color.Black; //set border color
TextBox1.ForeColor = System.Drawing.Color.Green; //set color

TextBox1.BorderStyle = BorderStyle.Dotted; //set border style
TextBox1.BorderWidth = 1; //set border width

TextBox1.Columns = 20; //set no of columns
TextBox1.CssClass = "someClass"; //apply css class
TextBox1.Enabled = true; //set if enabled

TextBox1.EnableViewState = true; //set if viewstate is true


TextBox1.Height = 50; //set height

TextBox1.MaxLength = 15;
TextBox1.ReadOnly = false; //set if readonly
TextBox1.Rows = 2; //set no of rows
TextBox1.TabIndex = 2; // set tabindex
TextBox1.Text = "default"; //set default text
TextBox1.TextMode = TextBoxMode.MultiLine; //set if textfield or textarea
TextBox1.ToolTip = "tip"; //set tootip
TextBox1.Visible = true; //set if visible
TextBox1.Width = 180; //set width
TextBox1.Wrap = true; //set if text appears wrapped

February 19, 2009


In ASP.NET, the textbox control is a web server control. This control allows
user to enter text. The text can then be validated and processed on the server
side.


The TextBox control can be used in a web page by simply dragging the TextBox in
the Toolbox > Standard section in Visual Web Developer. While dragging the
control, the following code is created


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


Note that the ID refers to the ID of the control and runat=”server” means that
the control will be run on the server side. By default, the control will be
rendered as a single line textbox similar to HTML
<input type="“text”" />
control.


To display the TextBox control as a HTML text-area control use the code

<
asp:TextBox

ID="TextBox" runat="server" TextMode="MultiLine"></asp:TextBox>


To display it as password text-field, use the following code

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


To set the width of the TextBox control, use either of the following code,

<
asp:TextBox ID="TextBox" runat="server" Width="100px"></asp:TextBox>

Or

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


To set the height of the TextBox control, use either of the following code,

<asp:TextBox ID="TextBox" runat="server" Rows="10"></asp:TextBox>

Or

<asp:TextBox ID="TextBox" runat="server" Height="100px"></asp:TextBox>


To set the border properties of the TextBox control, use the following code,

<asp:TextBox
ID="TextBox" runat="server" BorderColor="Red" BorderStyle="Dotted" BorderWidth="1px"></asp:TextBox>


The following border styles are available,
  • Dotted
  • Dashed
  • Double
  • Solid
  • Groove
  • Inset
  • Outset
  • None
  • Notset
  • Ridge



To set the font properties, use the following code,

<asp:TextBox ID="TextBox" runat="server" Font-Bold="true" Font-Italic="true" Font-Names="Trebuchet MS" Font-Size="Medium" Font-Underline="True" Strikeout="True"></asp:TextBox>


To set the fore and back color of the TextBox control, use the following code,

<asp:TextBox ID="TextBox" runat="server" BackColor="Red" ForeColor="Blue"></asp:TextBox>


To set the tab index, use the following code,

<asp:TextBox ID="TextBox" runat="server" TabIndex="4"></asp:TextBox>


To set the access key, use the following code,

<asp:TextBox ID="TextBox" runat="server" AccessKey="K"></asp:TextBox>


To set the AutoPostBack property, use the following line,

<asp:TextBox ID="TextBox" runat="server" AutoPostBack="true"></asp:TextBox>


To disable or enable the TextBox control, set the Enabled or ReadOnly property as follows.

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

Or

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


To enable or disable the ViewState, use the following code,

<asp:TextBox ID="TextBox" runat="server" EnableViewState="true"></asp:TextBox>


To set the maximum number of characters entered in the TextBox control, use the
following line,

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


To set the tool-tip property of the textbox, use the following code,

<asp:TextBox ID="TextBox" runat="server" ToolTip="tool tip"></asp:TextBox>
Reference: Shahed Kazi at AspNetify.com