ASP.Net/Asp Control/TextBox
Содержание
- 1 asp:TextBox: AccessKey (VB.net)
- 2 asp:textbox: back ground color (VB.net)
- 3 asp:TextBox: BorderStyle (VB.net)
- 4 asp:TextBox: format (VB.net)
- 5 asp: textbox on text changed event (C#)
- 6 asp:textbox: wrap (VB.net)
- 7 BackColor, ForeColor, BorderColor, AccessKey, Columns for asp:textbox (VB.net)
- 8 Control asp:TextBox visibility (VB.net)
- 9 Convert value from asp:TextBox to upper case (VB.net)
- 10 Create URL from textbox input
- 11 Get Text Box input and check if it is an Odd Number (VB.net)
- 12 Get value from TextBox (C#)
- 13 HTML Button, Select and InputBox in code behind (C#)
- 14 Is asp:textbox empty (C#)
- 15 MaxLength for asp:textbox (VB.net)
- 16 Multiline asp:TextBox (VB.net)
- 17 On text changed in asp:TextBox (VB.net)
- 18 Pre fill the asp textbox control (C#)
- 19 Rows, TextMode, Wrap
- 20 Set AutoCompleteType
- 21 Set MaxLength, Columns
- 22 Set TextBox focus
- 23 Single Line asp:TextBox (VB.net)
- 24 TabIndex for asp:textbox (VB.net)
- 25 TextMode=Password
- 26 TextMode: SingleLine, Password and MultiLine
- 27 Triggering an event when a TextBox change occurs (C#)
- 28 Triggering an event when a TextBox change occurs (VB)
- 29 Validate textbox value manually (C#)
- 30 Watch TextBox, CheckBox and radiobutton auto post back action (C#)
asp:TextBox: AccessKey (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
lblDataEntered.Text = "You entered:<BR>" _
& txtName.Text & "<BR>" _
& txtEmail.Text & "<BR>" _
& txtPhone.Text
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Basic TextBox Control</TITLE>
</HEAD>
<form runat="server">
<asp:Label
id="lblDataEntered"
runat="server"
/>
<BR><BR>
Enter your Name:<BR>
<asp:TextBox
id="txtName"
Columns="25"
MaxLength="30"
Text="Your Name Here"
runat=server
/>
<BR><BR>
Enter your Email Address:<BR>
<asp:TextBox
id="txtEmail"
Columns="35"
MaxLength="50"
Text="@mycompany.ru"
BackColor="LightYellow"
Font-Name="Comic Sans MS"
Font-Size="9pt"
Font-Bold="True"
ToolTip="Remember to include your full email address!"
runat=server
/>
<BR><BR>
Enter your Phone Number:<BR>
<asp:TextBox
id="txtPhone"
Columns="15"
MaxLength="15"
Text="() -"
AccessKey="P"
BackColor="DarkRed"
BorderWidth="3"
BorderColor="DarkBlue"
BorderStyle="Dashed"
ForeColor="Yellow"
ToolTip="No Extensions!"
runat=server
/>
<BR><BR>
<asp:button
id="butOK"
text="OK"
Type="Submit"
OnClick="SubmitBtn_Click"
runat="server"
/>
</Form>
</BODY>
</HTML>
asp:textbox: back ground color (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Multi-line TextBox Control</TITLE>
</HEAD>
<form runat="server">
Enter your comment:<BR>
<asp:textbox
id="txtComment1"
textmode="MultiLine"
columns="25"
rows="5"
wrap="True"
backcolor="LightYellow"
font-name="Comic Sans MS"
font-size="9pt"
runat=server
/>
<BR><BR>
Enter your comment:<BR>
<asp:textbox
id="txtComment2"
textmode="MultiLine"
columns="25"
rows="10"
wrap="False"
backcolor="LightYellow"
font-name="Comic Sans MS"
font-size="9pt"
runat=server
/>
<BR><BR>
</Form>
</BODY>
</HTML>
asp:TextBox: BorderStyle (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
lblDataEntered.Text = "You entered:<BR>" _
& txtName.Text & "<BR>" _
& txtEmail.Text & "<BR>" _
& txtPhone.Text
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Basic TextBox Control</TITLE>
</HEAD>
<form runat="server">
<asp:Label
id="lblDataEntered"
runat="server"
/>
<BR><BR>
Enter your Name:<BR>
<asp:TextBox
id="txtName"
Columns="25"
MaxLength="30"
Text="Your Name Here"
runat=server
/>
<BR><BR>
Enter your Email Address:<BR>
<asp:TextBox
id="txtEmail"
Columns="35"
MaxLength="50"
Text="@mycompany.ru"
BackColor="LightYellow"
Font-Name="Comic Sans MS"
Font-Size="9pt"
Font-Bold="True"
ToolTip="Remember to include your full email address!"
runat=server
/>
<BR><BR>
Enter your Phone Number:<BR>
<asp:TextBox
id="txtPhone"
Columns="15"
MaxLength="15"
Text="() -"
AccessKey="P"
BackColor="DarkRed"
BorderWidth="3"
BorderColor="DarkBlue"
BorderStyle="Dashed"
ForeColor="Yellow"
ToolTip="No Extensions!"
runat=server
/>
<BR><BR>
<asp:button
id="butOK"
text="OK"
Type="Submit"
OnClick="SubmitBtn_Click"
runat="server"
/>
</Form>
</BODY>
</HTML>
asp:TextBox: format (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
lblDataEntered.Text = "You entered:<BR>" _
& txtName.Text & "<BR>" _
& txtEmail.Text & "<BR>" _
& txtPhone.Text
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Basic TextBox Control</TITLE>
</HEAD>
<form runat="server">
<asp:Label
id="lblDataEntered"
runat="server"
/>
<BR><BR>
Enter your Name:<BR>
<asp:TextBox
id="txtName"
Columns="25"
MaxLength="30"
Text="Your Name Here"
runat=server
/>
<BR><BR>
Enter your Email Address:<BR>
<asp:TextBox
id="txtEmail"
Columns="35"
MaxLength="50"
Text="@mycompany.ru"
BackColor="LightYellow"
Font-Name="Comic Sans MS"
Font-Size="9pt"
Font-Bold="True"
ToolTip="Remember to include your full email address!"
runat=server
/>
<BR><BR>
Enter your Phone Number:<BR>
<asp:TextBox
id="txtPhone"
Columns="15"
MaxLength="15"
Text="() -"
AccessKey="P"
BackColor="DarkRed"
BorderWidth="3"
BorderColor="DarkBlue"
BorderStyle="Dashed"
ForeColor="Yellow"
ToolTip="No Extensions!"
runat=server
/>
<BR><BR>
<asp:button
id="butOK"
text="OK"
Type="Submit"
OnClick="SubmitBtn_Click"
runat="server"
/>
</Form>
</BODY>
</HTML>
asp: textbox on text changed event (C#)
<%@ Page Language="C#" %>
<script runat="server">
protected void txtSearch_TextChanged(object sender, EventArgs e)
{
lblSearchResults.Text = "Search for: " + txtSearch.Text;
}
</script>
<html>
<head>
<title>TextBox AutoPostBack</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label
id="lblSearch"
Text="Search:"
Runat="server" />
<asp:TextBox
id="txtSearch"
AutoPostBack="true"
OnTextChanged="txtSearch_TextChanged"
Runat="server" />
<hr />
<asp:Label
id="lblSearchResults"
Runat="server" />
</div>
</form>
</body>
</html>
asp:textbox: wrap (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Multi-line TextBox Control</TITLE>
</HEAD>
<form runat="server">
Enter your comment:<BR>
<asp:textbox
id="txtComment1"
textmode="MultiLine"
columns="25"
rows="5"
wrap="True"
backcolor="LightYellow"
font-name="Comic Sans MS"
font-size="9pt"
runat=server
/>
<BR><BR>
Enter your comment:<BR>
<asp:textbox
id="txtComment2"
textmode="MultiLine"
columns="25"
rows="10"
wrap="False"
backcolor="LightYellow"
font-name="Comic Sans MS"
font-size="9pt"
runat=server
/>
<BR><BR>
</Form>
</BODY>
</HTML>
BackColor, ForeColor, BorderColor, AccessKey, Columns for asp:textbox (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>TextBox Controls Sample Page</TITLE>
</HEAD>
<BODY >
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:TextBox
id="txtStandard"
Columns="25"
MaxLength="30"
Text="Single Line"
runat=server
/>
<BR><BR>
<asp:TextBox
id="txtPassword"
Columns="10"
MaxLength="30"
runat=server
TabIndex=1
Text="Password"
TextMode="Password"
/>
<BR><BR>
<asp:TextBox
id="txtMemo"
Columns="60"
Rows="5"
runat=server
Text="Multiple Lines"
TextMode="MultiLine"
Font-Name="Arial"
Font-Bold="True"
BackColor="LightYellow"
ForeColor="DarkRed"
BorderColor="DarkRed"
AccessKey="M"
/>
</Font>
</Form>
</BODY>
</HTML>
Control asp:TextBox visibility (VB.net)
<%@ Page Language="VB" %>
<script runat="server">
Sub Button1_Click(sender As Object, e As EventArgs)
Label3.Visible = False
Label1.Text = Int(Rnd() * 6) + 1
Label2.Text = Int(Rnd() * 6) + 1
Image2.ImageUrl = "http://www.nfex.ru/style/logo.png"
Image3.ImageUrl = "http://www.nfex.ru/style/logo.png"
If Label1.Text = "1" And Label2.Text = "1" Then
Label5.Text = Label4.Text
If CInt(Label5.Text) > CInt(Label6.Text) Then
Label6.Text = label5.Text
Panel1.Visible = True
End If
Label4.Text = 0
Label3.Visible = True
Else
Label4.Text = CInt(Label4.Text) + CInt(Label1.Text) + CInt(Label2.Text)
End If
End Sub
Sub Button2_Click(sender As Object, e As EventArgs)
Label6.Text = 0
End Sub
Sub Button3_Click(sender As Object, e As EventArgs)
Panel1.Visible = False
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="Label1" runat="server" Height="44px" Font-Size="X-Large" Font-Names="Verdana" BorderStyle="Solid">0</asp:Label>
<asp:Label id="Label2" runat="server" Height="44px" Font-Size="X-Large" Font-Names="Verdana" BorderStyle="Solid">0</asp:Label>
</p>
<p>
<asp:Image id="Image2" runat="server"></asp:Image>
<asp:Image id="Image3" runat="server"></asp:Image>
</p>
<p>
<asp:Label id="Label3" runat="server" Visible="False">Ooops, you did it again :(</asp:Label>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Hit me baby one more time..."></asp:Button>
</p>
<p>
<asp:Label id="Label4" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>
</p>
<p>
Last Score:
<asp:Label id="Label5" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>
</p>
<p>
High Score:
<asp:Label id="Label6" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>
</p>
<p>
<asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Reset High Scores"></asp:Button>
</p>
<p>
<asp:Panel id="Panel1" style="Z-INDEX: 100; LEFT: 192px; POSITION: absolute; TOP: 421px" runat="server" Visible="False">
<p>
Well done, you can now submit your high score.
</p>
<p>
Name:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
E-mail:
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="Button3" onclick="Button3_Click" runat="server" Text="Submit High Score"></asp:Button>
</p>
</asp:Panel>
</p>
</form>
</body>
</html>
Convert value from asp:TextBox to upper case (VB.net)
<%@ Page Language="VB" %>
<script runat="server">
Sub Button1_Click(sender As Object, e As EventArgs)
Label1.Text = (TextBox1.Text = TextBox2.Text)
Label2.Text = (TextBox1.Text.toUpper() = TextBox2.Text.toUpper())
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
String 1:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
String 2:
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
</p>
<p>
Case-sensitive:
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<p>
Case-<strong>in</strong>sensitive:
<asp:Label id="Label2" runat="server">Label</asp:Label>
</p>
</form>
</body>
</html>
Create URL from textbox input
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Email"></asp:Label><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Submit"/>
<asp:Button ID="Button2" runat="server" Text="Session" />
<asp:Button ID="Button3" runat="server" Text="Button" /><br />
</div>
</form>
</body>
</html>
File: Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Public Function Name() As String
Return TextBox1.Text
End Function
Public Function EMail() As String
Return TextBox2.Text
End Function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim URL As String
URL = "Default.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text
Response.Redirect(URL)
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Session("name") = TextBox1.Text
Session("email") = TextBox2.Text
Server.Transfer("Default2.aspx")
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
End Sub
End Class
Get Text Box input and check if it is an Odd Number (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
lblTitle.Text = "Odd or Even Page"
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
lblMessage2.Text = "number entered: " & OddOrEven(txtNumber.Text)
End Sub
Function OddOrEven(TheNumber as Long) as String
If TheNumber Mod 2 = 0 Then
OddOrEven = "even."
Else
OddOrEven = "odd."
End If
End Function
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Odd Or Even Sample Page</TITLE>
</HEAD>
<BODY>
<form runat="server">
<Font Face="Tahoma">
<asp:Label
id="lblTitle"
runat="server"
/>
<BR><BR><BR>
<asp:Label
id="lblMessage1"
runat="server"
Text="Enter a number"
/>
<asp:TextBox
id="txtNumber"
runat="server"
/>
<BR>
<asp:button
id="butOK"
text="OK"
Type="Submit"
OnClick="SubmitBtn_Click"
runat="server"
/>
<BR><BR>
<asp:Label
id="lblMessage2"
runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>
Get value from TextBox (C#)
<%@ Page Language="c#" %>
<script runat="server">
void Page_Load()
{
if (Page.IsPostBack)
{
lblName.Text = "";
lblAddress.Text = "";
lblPassword.Text = "";
}
if (txtName.Text !="")
lblName.Text = "You have entered the following name: " + txtName.Text;
if (txtAddress.Text !="")
lblAddress.Text = "You have entered the following address: " + txtAddress.Text;
if (txtPassword.Text !="")
lblPassword.Text = "You have entered the following password: " + txtPassword.Text;
}
</script>
<html>
<head>
<title>Text Box Example</title>
</head>
<body>
<asp:Label id="lblName" runat="server"></asp:Label>
<br />
<asp:Label id="lblAddress" runat="server"></asp:Label>
<br />
<asp:Label id="lblPassword" runat="server"></asp:Label>
<br />
<form runat="server">
Please enter your name:
<asp:textbox id="txtName" runat="server"></asp:textbox>
<br />
<br />
Please enter your address:
<asp:textbox id="txtAddress" runat="server" textmode="multiline" rows="5"></asp:textbox>
<br />
<br />
Please enter your password:
<asp:textbox id="txtPassword" runat="server" textmode="password"></asp:textbox>
<br />
<br />
<input type="submit" value="Submit Query" />
</form>
</body>
</html>
HTML Button, Select and InputBox in code behind (C#)
<%@ Page Language="C#"
Inherits="Page2"
Src="Page2.cs" AutoEventWireUp="false" %>
<html>
<body>
<form runat=server>
<h3>Enter name:
<input type=text id=_name runat=server/></h3>
<h3>Personality:
<select id=_personality runat=server /></h3>
<input type=button id=_enterButton
value="Enter" runat=server/>
<p runat=server id=_messageParagraph />
</form>
</body>
</html>
<%--
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Page2 : Page
{
protected HtmlSelect _personality;
protected HtmlInputText _name;
protected HtmlInputButton _enterButton;
protected HtmlGenericControl _messageParagraph;
override protected void OnInit(EventArgs e)
{
// Wire up handler to ServerClick event of button
_enterButton.ServerClick += new EventHandler(OnEnter);
}
override protected void OnLoad(EventArgs e)
{
// On initial access, populate select with items
if (!IsPostBack)
{
_personality.Items.Add(new ListItem("extraverted"));
_personality.Items.Add(new ListItem("introverted"));
_personality.Items.Add(new ListItem("in-between"));
}
}
protected void OnEnter(object src, EventArgs e)
{
// When the user presses enter, print a message
string msg = string.Format("Hi {0}, you selected {1}",
_name.Value, _personality.Value);
_messageParagraph.InnerText = msg;
}
}
--%>
Is asp:textbox empty (C#)
<script runat="server" language="C#">
void Page_Load()
{
Message1.Text = "";
Message2.Text = "";
Message3.Text = "";
if (text1.Text != "") {
Message1.Text = "You have entered the following name: " + text1.Text;
}
if (text2.Text != "") {
Message2.Text = "You have entered the following address: " + text2.Text;
}
if (text3.Text != "") {
Message3.Text = "You have entered the following password: " + text3.Text;
}
}
</script>
<html>
<head>
<title>Text Box Example</title>
</head>
<body>
<asp:label id="Message1" runat="server" />
<br />
<asp:label id="Message2" runat="server" />
<br />
<asp:label id="Message3" runat="server" />
<br />
<form runat="server">
Please enter your name:
<asp:textbox id="text1" runat="server" />
<br /><br />
Please enter your address:
<asp:textbox id="text2" runat="server" rows=5 textmode="multiline" />
<br /><br />
Please enter your chosen password:
<asp:textbox id="text3" runat="server" textmode="password" />
<br /><br />
<input type="Submit">
</form>
</body>
</html>
MaxLength for asp:textbox (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>TextBox Controls Sample Page</TITLE>
</HEAD>
<BODY >
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:TextBox
id="txtStandard"
Columns="25"
MaxLength="30"
Text="Single Line"
runat=server
/>
<BR><BR>
<asp:TextBox
id="txtPassword"
Columns="10"
MaxLength="30"
runat=server
TabIndex=1
Text="Password"
TextMode="Password"
/>
<BR><BR>
<asp:TextBox
id="txtMemo"
Columns="60"
Rows="5"
runat=server
Text="Multiple Lines"
TextMode="MultiLine"
Font-Name="Arial"
Font-Bold="True"
BackColor="LightYellow"
ForeColor="DarkRed"
BorderColor="DarkRed"
AccessKey="M"
/>
</Font>
</Form>
</BODY>
</HTML>
Multiline asp:TextBox (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<title>Input Control Example</title>
<script runat="server">
Sub Page_Load()
SingleText.Text = "Hello ASP.NET"
PasswordText.Attributes("Value") = "New Password"
MultiText.Text = "Multiline TextBox can hold many lines of text"
End Sub
</script>
</head>
<body>
<h1>Input Control Example</h1>
<form runat="server">
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<td>
Single Line TextBox:
</td>
<td>
<asp:textbox id="SingleText"
text="Single Line TextBox"
runat="server" />
</td>
</tr>
<tr>
<td>
Password TextBox:
</td>
<td>
<asp:textbox id="PasswordText"
text="Password"
textmode="Password"
runat="server" />
</td>
</tr>
<tr>
<td>
Multiline TextBox:
</td>
<td>
<asp:textbox id="MultiText"
text="Multiline TextBox"
textmode="Multiline"
runat="server" />
</td>
</tr>
</table>
</form>
</body>
</html>
On text changed in asp:TextBox (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub txtName_TextChanged(Sender As Object, E As EventArgs)
txtName.Text = UCase(txtName.Text)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Writing Code that Fires when Text in a TextBox Control Changes</TITLE>
</HEAD>
<form runat="server">
Enter your Name:<BR>
<asp:TextBox
id="txtName"
AutoPostBack=True
OnTextChanged="txtName_TextChanged"
Columns="25"
MaxLength="30"
Font-Name="Comic Sans MS"
Font-Size="9pt"
runat=server
/>
<BR><BR>
</Form>
</BODY>
</HTML>
Pre fill the asp textbox control (C#)
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load()
{
if (Page.IsPostBack) {
DateTime MyDate;
MyDate = Convert.ToDateTime(txtInput.Text);
lblOut.Text = Convert.ToString(MyDate);
}
}
</script>
<html>
<head>
<title>Creating Variables Example</title>
</head>
<body>
<form runat="server">
Please enter a date, such as 10/31/2006
<asp:TextBox runat="server" ID="txtInput">10/31/2006</asp:TextBox><br/>
<asp:Button runat="server" Text="Button"></asp:Button><br/>
Date is:
<asp:Label runat="server" ID="lblOut"/><br/>
</form>
</body>
</html>
Rows, TextMode, Wrap
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Example of TextBoxes</title>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
Comments:
<asp:TextBox ID="txtComments" runat="server" Rows="4" TextMode="MultiLine" Wrap="true"></asp:TextBox>
</div>
</form>
</body>
</html>
Set AutoCompleteType
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Example of TextBoxes</title>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
<p>
Name: <br />
<asp:TextBox ID="txtName" runat="server" AutoCompleteType="LastName"></asp:TextBox>
</p>
</div>
</form>
</body>
</html>
Set MaxLength, Columns
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Example of TextBoxes</title>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
<p>
State: <br />
<asp:TextBox ID="txtState" runat="server" MaxLength="2" Columns="2" AutoCompleteType="HomeCountryRegion"></asp:TextBox>
</p>
</div>
</form>
</body>
</html>
Set TextBox focus
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="FocusAndDefault" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Focus and Default Button</title>
</head>
<body>
<form id="Form1"
defaultbutton="cmdSubmit"
defaultfocus="TextBox1"
runat="server">
<div>
TextBox1:
<asp:textbox id="TextBox1"
runat="server"
AccessKey="1"></asp:textbox>
TextBox2:
<asp:textbox id="TextBox2"
runat="server"
AccessKey="2"></asp:textbox>
<asp:button id="cmdSetFocus1"
text="Set Focus #1"
runat="server"
OnClick="cmdSetFocus1_Click">
</asp:button>
<asp:button id="cmdSetFocus2"
text="Set Focus #2"
runat="server"
OnClick="cmdSetFocus2_Click" >
</asp:button>
<asp:Button ID="cmdSubmit"
runat="server"
Text="Submit"
OnClick="cmdSubmit_Click" />
<asp:label id="Label1"
runat="Server"
EnableViewState="False"></asp:label>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class FocusAndDefault : System.Web.UI.Page
{
protected void cmdSubmit_Click(object sender, EventArgs e)
{
Label1.Text = "Clicked Submit.";
}
protected void cmdSetFocus1_Click(object sender, EventArgs e)
{
TextBox1.Focus();
}
protected void cmdSetFocus2_Click(object sender, EventArgs e)
{
TextBox2.Focus();
}
}
Single Line asp:TextBox (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<title>Input Control Example</title>
<script runat="server">
Sub Page_Load()
SingleText.Text = "Hello ASP.NET"
PasswordText.Attributes("Value") = "New Password"
MultiText.Text = "Multiline TextBox can hold many lines of text"
End Sub
</script>
</head>
<body>
<h1>Input Control Example</h1>
<form runat="server">
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<td>
Single Line TextBox:
</td>
<td>
<asp:textbox id="SingleText"
text="Single Line TextBox"
runat="server" />
</td>
</tr>
<tr>
<td>
Password TextBox:
</td>
<td>
<asp:textbox id="PasswordText"
text="Password"
textmode="Password"
runat="server" />
</td>
</tr>
<tr>
<td>
Multiline TextBox:
</td>
<td>
<asp:textbox id="MultiText"
text="Multiline TextBox"
textmode="Multiline"
runat="server" />
</td>
</tr>
</table>
</form>
</body>
</html>
TabIndex for asp:textbox (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>TextBox Controls Sample Page</TITLE>
</HEAD>
<BODY >
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:TextBox
id="txtStandard"
Columns="25"
MaxLength="30"
Text="Single Line"
runat=server
/>
<BR><BR>
<asp:TextBox
id="txtPassword"
Columns="10"
MaxLength="30"
runat=server
TabIndex=1
Text="Password"
TextMode="Password"
/>
<BR><BR>
<asp:TextBox
id="txtMemo"
Columns="60"
Rows="5"
runat=server
Text="Multiple Lines"
TextMode="MultiLine"
Font-Name="Arial"
Font-Bold="True"
BackColor="LightYellow"
ForeColor="DarkRed"
BorderColor="DarkRed"
AccessKey="M"
/>
</Font>
</Form>
</BODY>
</HTML>
TextMode=Password
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Example of TextBoxes</title>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
Password:
<asp:TextBox ID="txtPass" runat="server" TextMode="Password"></asp:TextBox>
</div>
</form>
</body>
</html>
TextMode: SingleLine, Password and MultiLine
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show TextBox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox
id="txtUserName"
TextMode="SingleLine"
Runat="server" />
<asp:TextBox
id="txtPassword"
TextMode="Password"
Runat="server" />
<asp:TextBox
id="txtComments"
TextMode="MultiLine"
Runat="server" />
</div>
</form>
</body>
</html>
Triggering an event when a TextBox change occurs (C#)
<%@ Page Language="C#" %>
<script runat="server">
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write("OnTextChanged event triggered");
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("OnClick event triggered");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>OnTextChanged Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1"
Runat="server"
AutoPostBack="True"
OnTextChanged="TextBox1_TextChanged"/>
<asp:Button ID="Button1"
Runat="server"
Text="Button"
OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Triggering an event when a TextBox change occurs (VB)
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub TextBox1_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs)
Response.Write("OnTextChanged event triggered")
End Sub
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Response.Write("OnClick event triggered")
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>OnTextChanged Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Runat="server" AutoPostBack="True"
OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Button ID="Button1" Runat="server" Text="Button"
OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Validate textbox value manually (C#)
<%@ Page Language="c#" Debug="true" %>
<script Language="c#" runat="server">
void CompleteOrder(object sender, EventArgs e)
{
if (txtQuantity.Text != "")
{
if (!(Char.IsNumber(txtQuantity.Text,0)))
{
if (txtQuantity.Text.Substring(0,1) != "-")
{
lblOrderConfirm.Text = "Please provide only numbers in Quantity field.";
}
else
{
lblOrderConfirm.Text = "Please provide a Quantity greater than 0.";
}
}
else if (Convert.ToInt32(txtQuantity.Text) == 0)
{
lblOrderConfirm.Text = "Please provide a Quantity greater than 0.";
}
else if (Convert.ToInt32(txtQuantity.Text) > 0)
{
lblOrderConfirm.Text = "Order Successfully placed.";
}
}
else
{
lblOrderConfirm.Text = "Please provide an Order Quantity.";
}
}
</script>
<html>
<head>
<title>Manual Trapping Example</title>
</head>
<body>
<form method="post" action="manualtrapping.aspx" runat="server">
<asp:Label text="Order Quantity" runat="server" />
<asp:TextBox id="txtQuantity" runat="server" />
<br />
<asp:Button id="btnComplete_Order" Text="Complete Order"
onclick="CompleteOrder"
runat="server"/>
<br />
<asp:Label id="lblOrderConfirm" runat="server"/>
</form>
</body>
</html>
Watch TextBox, CheckBox and radiobutton auto post back action (C#)
<%@ Page language="c#" src="EventTracker.aspx.cs" AutoEventWireup="false" Inherits="EventTracker" %>
<HTML>
<body>
<form id="Form1" method="post" runat="server">
<P><h3>Controls being monitored for change events:</h3>
<asp:TextBox id="txt" runat="server" AutoPostBack="True">Text box auto post back</asp:TextBox><BR>
<BR>
<asp:CheckBox id="chk" runat="server" AutoPostBack="True"></asp:CheckBox><BR>
<BR>
<asp:RadioButton id="opt1" runat="server" GroupName="Sample" AutoPostBack="True"></asp:RadioButton>
<asp:RadioButton id="opt2" runat="server" GroupName="Sample" AutoPostBack="True"></asp:RadioButton><BR>
<BR>
<BR>
<h3>List of events:</h3>
<P></P>
<P>
<asp:ListBox id="lstEvents" runat="server" Width="355px" Height="256px"></asp:ListBox><BR>
</P>
</form>
</body>
</HTML>
<%--
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class EventTracker : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txt;
protected System.Web.UI.WebControls.CheckBox chk;
protected System.Web.UI.WebControls.RadioButton opt1;
protected System.Web.UI.WebControls.RadioButton opt2;
protected System.Web.UI.WebControls.ListBox lstEvents;
private void Page_Load(object sender, System.EventArgs e)
{
Log("<< Page_Load >>");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txt.TextChanged += new System.EventHandler(this.CtrlChanged);
this.chk.CheckedChanged += new System.EventHandler(this.CtrlChanged);
this.opt1.CheckedChanged += new System.EventHandler(this.CtrlChanged);
this.opt2.CheckedChanged += new System.EventHandler(this.CtrlChanged);
this.Load += new System.EventHandler(this.Page_Load);
this.PreRender += new System.EventHandler(this.EventTracker_PreRender);
}
#endregion
private void Log(string entry)
{
lstEvents.Items.Add(entry);
// Select the last item to scroll the list so the most recent
// entries are visible.
lstEvents.SelectedIndex = lstEvents.Items.Count - 1;
}
private void EventTracker_PreRender(object sender, System.EventArgs e)
{
// When the Page.UnLoad event occurs it is too late
// to change the list.
Log("Page_PreRender");
}
private void CtrlChanged(Object sender, EventArgs e)
{
// Find the control ID of the sender.
// This requires converting the Object type into a Control class.
string ctrlName = ((Control)sender).ID;
Log(ctrlName + " Changed");
}
}
--%>