TextBox Example in ASP.NET using C#

TextBox Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 27, 2010 at 5:43 PM Comments : [ 0 ]

In this article we will introduce with TextBox control in ASP.NET using C#.

TextBox Example in ASP.NET using C#

TextBox control in ASP.NET is used to write the text, number or special character. Basically TextBox control understand all these values as a string. You can set the appearance of TextBox by using CSS or through inline style.
In this example we insert the user information. When we enter the value in TextBox and click on other TextBox for enter the value then the result will be shown in label because the AutoPostBack="true" for every textbox. TextBox has only one event which is TextChanged. TextBox has a TextMode property. It has three value which are following.

1. SingleLine: The text will be displayed in a single line.
2. MultiLine: The text will be displayed in multiple line.
3. Password: The text will be displayed in password format.

TextBox.aspx (Design Page):

TextBox.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true"
CodeFile="TextBox.aspx.cs" Inherits="TextBox" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<style type="text/css">
.style2
{
font-size: x-large;
}
.txtStyle
{
background-color: #FFFFCC;
border-color: Silver;
font-family: Verdana;
border-style: solid;
}
.LabelStyle
{
font-family: Verdana;
color: #000099;
font-size: small;
font-weight: bold;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<h2 style="color: Green">
TextBox in ASP.NET 4 , C#</h2>
<span class="style2">User Information: </span>
<br /><br />
<strong>Name: </strong>
<br />
<asp:TextBox ID="nameTxt" runat="server" 
CssClass="txtStyle" AutoPostBack="true"></asp:TextBox>
<br />
<strong>Age:</strong>
<br />
<asp:TextBox ID="ageTxt" runat="server" 
CssClass="txtStyle" AutoPostBack="true"></asp:TextBox>
<br />
<strong>Post: </strong>
<br />
<asp:TextBox ID="postTxt" runat="server" 
CssClass="txtStyle" AutoPostBack="true"></asp:TextBox>
<br />
<strong>Address: </strong>
<br />
<asp:TextBox ID="addressTxt" runat="server" 
CssClass="txtStyle" AutoPostBack="true"></asp:TextBox>
<br />
<strong>Contact no.: </strong>
<br />
<asp:TextBox ID="contactTxt" runat="server" 
CssClass="txtStyle" AutoPostBack="true"></asp:TextBox>
<br />
<strong>Email-id: </strong>
<br />
<asp:TextBox ID="emailTxt" runat="server" 
CssClass="txtStyle" AutoPostBack="true"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label1" runat="server" CssClass="LabelStyle" />
<br />
<asp:Label ID="Label2" runat="server" CssClass="LabelStyle" />
<br />
<asp:Label ID="Label3" runat="server" CssClass="LabelStyle" />
<br />
<asp:Label ID="Label4" runat="server" CssClass="LabelStyle" />
<br />
<asp:Label ID="Label5" runat="server" CssClass="LabelStyle" />
<br />
<asp:Label ID="Label6" runat="server" CssClass="LabelStyle" />
</div>
</asp:Content>

TextBox.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TextBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
Label1.Text = "My name is " + nameTxt.Text;
Label2.Text = "I am " + ageTxt.Text + " years old";
Label3.Text = "I am a " + postTxt.Text;
Label4.Text = "I live in " + addressTxt.Text;
Label5.Text = "You contact me on " + contactTxt.Text;
Label6.Text = "and My Email-id is " + emailTxt.Text;
}
}
protected void submitBtn_Click(object sender, EventArgs e)
{ }
}

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics