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 Input Control Example
- 18 Input Control Example
- 18.1 TabIndex for asp:textbox (VB.net)
- 18.2 TextMode=Password
- 18.3 TextMode: SingleLine, Password and MultiLine
- 18.4 Triggering an event when a TextBox change occurs (C#)
- 18.5 Triggering an event when a TextBox change occurs (VB)
- 18.6 Validate textbox value manually (C#)
- 18.7 Watch TextBox, CheckBox and radiobutton auto post back action (C#)
asp:TextBox: AccessKey (VB.net)
<source lang="csharp">
<%@ 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:
" _ & txtName.Text & "
" _ & txtEmail.Text & "
" _ & txtPhone.Text
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Creating a Basic TextBox Control</TITLE> </HEAD> <form runat="server"> <asp:Label
id="lblDataEntered" runat="server"
/>
Enter your Name:
<asp:TextBox
id="txtName" Columns="25" MaxLength="30" Text="Your Name Here" runat=server
/>
Enter your Email Address:
<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
/>
Enter your Phone Number:
<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
/>
<asp:button
id="butOK" text="OK" Type="Submit" OnClick="SubmitBtn_Click" runat="server"
/> </Form> </BODY> </HTML>
</source>
asp:textbox: back ground color (VB.net)
<source lang="csharp">
<%@ 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:
<asp:textbox
id="txtComment1" textmode="MultiLine" columns="25" rows="5" wrap="True" backcolor="LightYellow" font-name="Comic Sans MS" font-size="9pt" runat=server
/>
Enter your comment:
<asp:textbox
id="txtComment2" textmode="MultiLine" columns="25" rows="10" wrap="False" backcolor="LightYellow" font-name="Comic Sans MS" font-size="9pt" runat=server
/>
</Form>
</BODY>
</HTML>
</source>
asp:TextBox: BorderStyle (VB.net)
<source lang="csharp">
<%@ 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:
" _ & txtName.Text & "
" _ & txtEmail.Text & "
" _ & txtPhone.Text
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Creating a Basic TextBox Control</TITLE> </HEAD> <form runat="server"> <asp:Label
id="lblDataEntered" runat="server"
/>
Enter your Name:
<asp:TextBox
id="txtName" Columns="25" MaxLength="30" Text="Your Name Here" runat=server
/>
Enter your Email Address:
<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
/>
Enter your Phone Number:
<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
/>
<asp:button
id="butOK" text="OK" Type="Submit" OnClick="SubmitBtn_Click" runat="server"
/> </Form> </BODY> </HTML>
</source>
asp:TextBox: format (VB.net)
<source lang="csharp">
<%@ 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:
" _ & txtName.Text & "
" _ & txtEmail.Text & "
" _ & txtPhone.Text
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Creating a Basic TextBox Control</TITLE> </HEAD> <form runat="server"> <asp:Label
id="lblDataEntered" runat="server"
/>
Enter your Name:
<asp:TextBox
id="txtName" Columns="25" MaxLength="30" Text="Your Name Here" runat=server
/>
Enter your Email Address:
<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
/>
Enter your Phone Number:
<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
/>
<asp:button
id="butOK" text="OK" Type="Submit" OnClick="SubmitBtn_Click" runat="server"
/> </Form> </BODY> </HTML>
</source>
asp: textbox on text changed event (C#)
<source lang="csharp">
<%@ 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">
<asp:Label id="lblSearch" Text="Search:" Runat="server" /> <asp:TextBox id="txtSearch" AutoPostBack="true" OnTextChanged="txtSearch_TextChanged" Runat="server" />
<asp:Label id="lblSearchResults" Runat="server" />
</form>
</body> </html>
</source>
asp:textbox: wrap (VB.net)
<source lang="csharp">
<%@ 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:
<asp:textbox
id="txtComment1" textmode="MultiLine" columns="25" rows="5" wrap="True" backcolor="LightYellow" font-name="Comic Sans MS" font-size="9pt" runat=server
/>
Enter your comment:
<asp:textbox
id="txtComment2" textmode="MultiLine" columns="25" rows="10" wrap="False" backcolor="LightYellow" font-name="Comic Sans MS" font-size="9pt" runat=server
/>
</Form>
</BODY>
</HTML>
</source>
BackColor, ForeColor, BorderColor, AccessKey, Columns for asp:textbox (VB.net)
<source lang="csharp">
<%@ 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"> <asp:TextBox
id="txtStandard" Columns="25" MaxLength="30" Text="Single Line" runat=server
/>
<asp:TextBox
id="txtPassword" Columns="10" MaxLength="30" runat=server TabIndex=1 Text="Password" TextMode="Password"
/>
<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"
/> </Form> </BODY> </HTML>
</source>
Control asp:TextBox visibility (VB.net)
<source lang="csharp">
<%@ 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">
<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>
<asp:Image id="Image2" runat="server"></asp:Image> <asp:Image id="Image3" runat="server"></asp:Image>
<asp:Label id="Label3" runat="server" Visible="False">Ooops, you did it again :(</asp:Label>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Hit me baby one more time..."></asp:Button>
<asp:Label id="Label4" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>
Last Score: <asp:Label id="Label5" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>
High Score: <asp:Label id="Label6" runat="server" Font-Size="X-Large" Font-Names="Verdana" ForeColor="Red">0</asp:Label>
<asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Reset High Scores"></asp:Button>
<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.
Name: <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
E-mail: <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:Button id="Button3" onclick="Button3_Click" runat="server" Text="Submit High Score"></asp:Button>
</asp:Panel> </p> </form>
</body> </html>
</source>
Convert value from asp:TextBox to upper case (VB.net)
<source lang="csharp">
<%@ 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">
String 1: <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
String 2: <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
Case-sensitive: <asp:Label id="Label1" runat="server">Label</asp:Label>
Case-insensitive: <asp:Label id="Label2" runat="server">Label</asp:Label>
</form>
</body> </html>
</source>
Create URL from textbox input
<source lang="csharp">
<%@ 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">
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Email"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit"/> <asp:Button ID="Button2" runat="server" Text="Session" /> <asp:Button ID="Button3" runat="server" Text="Button" />
</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
</source>
Get Text Box input and check if it is an Odd Number (VB.net)
<source lang="csharp">
<%@ 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"> <asp:Label
id="lblTitle" runat="server"
/>
<asp:Label
id="lblMessage1" runat="server" Text="Enter a number"
/> <asp:TextBox
id="txtNumber" runat="server"
/>
<asp:button
id="butOK" text="OK" Type="Submit" OnClick="SubmitBtn_Click" runat="server"
/>
<asp:Label
id="lblMessage2" runat="server"
/> </Form> </BODY> </HTML>
</source>
Get value from TextBox (C#)
<source lang="csharp">
<%@ 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>
<asp:Label id="lblAddress" runat="server"></asp:Label>
<asp:Label id="lblPassword" runat="server"></asp:Label>
<form runat="server"> Please enter your name: <asp:textbox id="txtName" runat="server"></asp:textbox>
Please enter your address: <asp:textbox id="txtAddress" runat="server" textmode="multiline" rows="5"></asp:textbox>
Please enter your password: <asp:textbox id="txtPassword" runat="server" textmode="password"></asp:textbox>
<input type="submit" value="Submit Query" /> </form>
</body> </html>
</source>
HTML Button, Select and InputBox in code behind (C#)
<source lang="csharp">
<%@ Page Language="C#"
Inherits="Page2" Src="Page2.cs" AutoEventWireUp="false" %>
<html> <body>
<form runat=server>
Enter name: <input type=text id=_name runat=server/>
Personality: <select id=_personality runat=server />
<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; }
}
--%>
</source>
Is asp:textbox empty (C#)
<source lang="csharp">
<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" />
<asp:label id="Message2" runat="server" />
<asp:label id="Message3" runat="server" />
<form runat="server"> Please enter your name: <asp:textbox id="text1" runat="server" />
Please enter your address: <asp:textbox id="text2" runat="server" rows=5 textmode="multiline" />
Please enter your chosen password: <asp:textbox id="text3" runat="server" textmode="password" />
<input type="Submit"> </form> </body>
</html>
</source>
MaxLength for asp:textbox (VB.net)
<source lang="csharp">
<%@ 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"> <asp:TextBox
id="txtStandard" Columns="25" MaxLength="30" Text="Single Line" runat=server
/>
<asp:TextBox
id="txtPassword" Columns="10" MaxLength="30" runat=server TabIndex=1 Text="Password" TextMode="Password"
/>
<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"
/> </Form> </BODY> </HTML>
</source>
Multiline asp:TextBox (VB.net)
<source lang="csharp">
<%@ 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>
Input Control Example
<form runat="server">
Single Line TextBox: |
<asp:textbox id="SingleText" text="Single Line TextBox" runat="server" /> |
Password TextBox: |
<asp:textbox id="PasswordText" text="Password" textmode="Password" runat="server" /> |
Multiline TextBox: |
<asp:textbox id="MultiText" text="Multiline TextBox" textmode="Multiline" runat="server" /> |
</form>
</body> </html>
</source>
On text changed in asp:TextBox (VB.net)
<source lang="csharp">
<%@ 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:
<asp:TextBox
id="txtName" AutoPostBack=True OnTextChanged="txtName_TextChanged" Columns="25" MaxLength="30" Font-Name="Comic Sans MS" Font-Size="9pt" runat=server
/>
</Form>
</BODY>
</HTML>
</source>
Pre fill the asp textbox control (C#)
<source lang="csharp">
<%@ 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>
<asp:Button runat="server" Text="Button"></asp:Button>
Date is: <asp:Label runat="server" ID="lblOut"/>
</form>
</body> </html>
</source>
Rows, TextMode, Wrap
<source lang="csharp">
<%@ 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">
Comments: <asp:TextBox ID="txtComments" runat="server" Rows="4" TextMode="MultiLine" Wrap="true"></asp:TextBox>
</form>
</body> </html>
</source>
Set AutoCompleteType
<source lang="csharp">
<%@ 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">
Name:
<asp:TextBox ID="txtName" runat="server" AutoCompleteType="LastName"></asp:TextBox>
</form>
</body> </html>
</source>
Set MaxLength, Columns
<source lang="csharp">
<%@ 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">
State:
<asp:TextBox ID="txtState" runat="server" MaxLength="2" Columns="2" AutoCompleteType="HomeCountryRegion"></asp:TextBox>
</form>
</body> </html>
</source>
Set TextBox focus
<source lang="csharp">
<%@ 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">
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>
</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(); }
}
</source>
Single Line asp:TextBox (VB.net)
<source lang="csharp">
<%@ 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>
Input Control Example
<form runat="server">
Single Line TextBox: |
<asp:textbox id="SingleText" text="Single Line TextBox" runat="server" /> |
Password TextBox: |
<asp:textbox id="PasswordText" text="Password" textmode="Password" runat="server" /> |
Multiline TextBox: |
<asp:textbox id="MultiText" text="Multiline TextBox" textmode="Multiline" runat="server" /> |
</form>
</body> </html>
</source>
TabIndex for asp:textbox (VB.net)
<source lang="csharp">
<%@ 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"> <asp:TextBox
id="txtStandard" Columns="25" MaxLength="30" Text="Single Line" runat=server
/>
<asp:TextBox
id="txtPassword" Columns="10" MaxLength="30" runat=server TabIndex=1 Text="Password" TextMode="Password"
/>
<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"
/> </Form> </BODY> </HTML>
</source>
TextMode=Password
<source lang="csharp">
<%@ 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">
Password: <asp:TextBox ID="txtPass" runat="server" TextMode="Password"></asp:TextBox>
</form>
</body> </html>
</source>
TextMode: SingleLine, Password and MultiLine
<source lang="csharp">
<%@ 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">
<asp:TextBox id="txtUserName" TextMode="SingleLine" Runat="server" /> <asp:TextBox id="txtPassword" TextMode="Password" Runat="server" /> <asp:TextBox id="txtComments" TextMode="MultiLine" Runat="server" />
</form>
</body> </html>
</source>
Triggering an event when a TextBox change occurs (C#)
<source lang="csharp">
<%@ 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">
<asp:TextBox ID="TextBox1" Runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"/> <asp:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body> </html>
</source>
Triggering an event when a TextBox change occurs (VB)
<source lang="csharp">
<%@ 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">
<asp:TextBox ID="TextBox1" Runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox> <asp:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body> </html>
</source>
Validate textbox value manually (C#)
<source lang="csharp">
<%@ 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" />
<asp:Button id="btnComplete_Order" Text="Complete Order" onclick="CompleteOrder" runat="server"/>
<asp:Label id="lblOrderConfirm" runat="server"/> </form> </body>
</html>
</source>
Watch TextBox, CheckBox and radiobutton auto post back action (C#)
<source lang="csharp">
<%@ Page language="c#" src="EventTracker.aspx.cs" AutoEventWireup="false" Inherits="EventTracker" %> <HTML>
<body> <form id="Form1" method="post" runat="server">
Controls being monitored for change events:
<asp:TextBox id="txt" runat="server" AutoPostBack="True">Text box auto post back</asp:TextBox>
<asp:CheckBox id="chk" runat="server" AutoPostBack="True"></asp:CheckBox>
<asp:RadioButton id="opt1" runat="server" GroupName="Sample" AutoPostBack="True"></asp:RadioButton> <asp:RadioButton id="opt2" runat="server" GroupName="Sample" AutoPostBack="True"></asp:RadioButton>
List of events:
<P>
<asp:ListBox id="lstEvents" runat="server" Width="355px" Height="256px"></asp:ListBox>
</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"); } }
--%>
</source>