Monday, 7 May 2012

Textbox

How to Use a Textbox in Asp.Net:

Textbox is mostly used to take input from a user. e.g; user name,address etc.
To use a textbox first open toolbox and double click on Textbox or drag it to web page.
Then the following text will be appear on your web page.
       
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

In the previous text  ID is used to set identifier for your tag. Remember that asp tags is by default runat server if you will remove runat server then asp tag will not be execute.
Now run the file you will see a textbox will be placed on your website.
Some Useful Properties of Textbox: ID Property: Textbox.ID property id used to set the ID of textbox control. protected void Page_Load(object sender, System.EventArgs e)
{    // Set a ID to a Textbox    textbox1.ID = "txtUserName";
}

Text Property: We can get value from textbox by using TextboxName.Text property and set a value to textbox by using TextboxName.Text property.For example
protected void Page_Load(object sender, System.EventArgs e)
{    // Get a value from Textbox
   string userName = TextBox1.Text;    //Set a value to Textbox    textbox.Text = userName;
}

protected void Button1_Click(object sender, System.EventArgs e)
{
   TextBox2.Text = TextBox1.Text;
}
Readonly Property: Textbox.Readonly property set the textbox behaviour as readonly.Inother words user can only see the contents and can't modify but still copy the contents of a textbox.Initially textbox.Readonly property set to false;
protected void Button1_Click(object sender, System.EventArgs e)
{
   TextBox2.Readonly = true;
}

Enabled Property:
Textbox.Enabled property anable the state of textbox control.It is similar to Readonly property but the difference is that you can copy the contents by Readonly property but with Enabled property you even can't copy the contents.Initially its value is true.
protected void Button1_Click(object sender, System.EventArgs e)
{
   TextBox2.Text = false;
}

Textmode Property:
Gets or Set the behaviour mode of a textbox such that Songleline, Password, Multiline;
protected void Button1_Click(object sender, System.EventArgs e)
{
   TextBox1.Text = "Singleline"
}

Visible Property:
Textbox.Visible property is used to set the visiblity of a textbox.If its value is true then textbox will be visible otherwise it will be hide for user.
protected void Button1_Click(object sender, System.EventArgs e)
{
   TextBox1.Text = false;
}

Height & Width Property of Textbox: You can use the Height property along with the Width property  to size an object to specific  dimensions.
protected void Button1_Click(object sender, System.EventArgs e)
{
   TextBox1.Height = 100px;    Textbox1.Width = 100px;
}

Some Useful Events of Textbox
TextboxChanged Event: How to Respond to Changes in a TextBox ? By default, the TextChanged event does not immediately cause the Web Forms page to be post back to the      server. Infact, the event is raised in webserver code the next time the form is posted. To have the TextChanged event cause an immediate posting, set the TextBox control's Autopostback property to true.
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   Label1.Text = TextBox1.Text;
}

Load Event:

This event fires when the web page has been loaded.
protected void Textbox1_Load(object sender, System.EventArgs e)
{
   Here You can write your code.
}
Some Useful Methods of Textbox

Focus Method:

How to Set Focus on Asp.Net  ?

Textbox.Focus() method is use to set the control on a Textbox.Focus method is helpful to reduce the use of mouse and user can easily get a control by using keyboard. AT Server Side : 
protected void Page_Load(object sender, EventArgs e)
{
    TextBox1.Focus();
} At Client Side:
<%@ Page Language="C#" %>
<html>
<head runat="server">
  <title>Test Page</title>
</head>
<body>
  <form id="form1" runat="server" defaultfocus="TextBox1" >
    <div>
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <br />
      <asp:Button ID="Button1" runat="server" Text="Button" />
      <br />
    </div>
  </form>
</body>
</html> Databind() Method: Textbox.Databind() binds the datasource to the invoked server control and all its child controls. Dispose() Method: Textbox.Dispose()  enables a server control to perform a final cleanup before it is released from memory. This video tutorial defines how to use textbox with different styles:
To see more properties about textbox click on following link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.aspx

No comments:

Post a Comment