ASP.Net/Asp Control/Label
Содержание
- 1 asp:Button action to change the asp:Label and generate random number (VB.net)
- 2 Snake Eyes!
- 3 Control Properties Example
- 3.1 asp:label: border, font and background color
- 3.2 Asp server Label: display server time (C#)
- 3.3 Assign current time to asp label (C#)
- 3.4 Boolean value toggle: label Visible (C#)
- 3.5 BorderWidth, BorderColor, Font-Size, Font-Name, ForeColor, BackColor , ToolTip, Visible for asp:Label (VB.net)
- 3.6 Change asp label text in asp button action (C#)
- 3.7 Change Label to be visible (C#)
- 3.8 Define function to change Label Font to Italic (C#)
- 3.9 Display all page event in asp label (C#)
- 3.10 Do a simple calculation and set value to asp:Label (VB.net)
- 3.11 Label"s color
- 3.12 Set Asp Label Text during page load (C#)
- 3.13 Set asp:Label text (VB.net)
- 3.14 Set color and font for an asp:Label (VB.net)
- 3.15 Set Label background color (C#)
- 3.16 Set Label style (VB.net)
- 4 Control Properties Example
asp:Button action to change the asp:Label and generate random number (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 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 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
</script> <html> <head> </head> <body>
<form runat="server">
Snake Eyes!
Keep going, but don"t get two 1"s...
<asp:Label id="Label1" runat="server" BorderStyle="Solid" Font-Names="Verdana" Font-Size="X-Large" Height="44px">0</asp:Label> <asp:Label id="Label2" runat="server" BorderStyle="Solid" Font-Names="Verdana" Font-Size="X-Large" Height="44px">0</asp:Label>
<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-Names="Verdana" Font-Size="X-Large" ForeColor="Red">0</asp:Label>
Last Score:<asp:Label id="Label5" runat="server" Font-Names="Verdana" Font-Size="X-Large" ForeColor="Red">0</asp:Label>
High Score: <asp:Label id="Label6" runat="server" Font-Names="Verdana" Font-Size="X-Large" ForeColor="Red">0</asp:Label>
<asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Reset High Scores"></asp:Button>
</form>
</body> </html>
</source>
asp:Label background color (VB.net)
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<title>Control Properties Example</title> <script runat="server"> Sub Page_Load() Label2.BackColor = System.Drawing.Color.LightBlue End Sub </script>
</head> <body>
Control Properties Example
<form runat="server"> <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server"> <asp:tablerow runat="server"> <asp:tablecell runat="server"> Default Label: </asp:tablecell> <asp:tablecell runat="server"> <asp:label id="Label1" runat="server"> Hello, World! </asp:label> </asp:tablecell> </asp:tablerow> <asp:tablerow runat="server"> <asp:tablecell runat="server"> Label with Properties: </asp:tablecell> <asp:tablecell runat="server"> <asp:label id="Label2" font-name="arial" runat="server"> Hello, World! </asp:label> </asp:tablecell> </asp:tablerow> </asp:table> </form>
</body> </html>
</source>
asp:label: border, font and background color
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs) End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Creating a Basic Label Control</TITLE> </HEAD> <form runat="server"> <asp:label
id="lblTitle" backcolor="LightGreen" borderwidth="7px" borderstyle=6 width="90%" font-size="20pt" font-name="Lucida Console"text="
Our Site
runat="server"
/>
<asp:label
id="lblMessage1" backcolor="LightYellow" font-name="Comic Sans MS" font-size="12pt" font-italic="True" tooltip="Mouse hovering text." text="Welcome to our site." runat="server"
/>
<asp:label
id="lblMessage2" font-name="Comic Sans MS" font-size="12pt" font-overline="True" text="A message with a line above it." runat="server"
/>
<asp:label
id="lblMessage3" font-name="Comic Sans MS" font-size="12pt" font-strikeout="True" text="A message with a line drawn through it." runat="server"
/> </form> </BODY> </HTML>
</source>
Asp server Label: display server time (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
void Page_Load() { time.Text=DateTime.Now.ToString(); }
</script> <html> <head> </head> <body>
<form runat="server"> In WebServerLand the time is currently:<asp:Label id="time" runat="server" /> </form>
</body> </html>
</source>
Assign current time to asp label (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
void Page_Load() { lblServerTime.Text = DateTime.Now.ToString(); }
</script> <html> <head>
<title>First Page</title>
</head> <body>
<form id="form1" runat="server">
Welcome to ASP.NET 2.0! The current date and time is: <asp:Label id="lblServerTime" Runat="server" />
</form>
</body> </html>
</source>
Boolean value toggle: label Visible (C#)
<source lang="csharp">
<%@ Page Language="C#" Debug="true" %> <script runat="server">
void Page_Load() { if (Page.IsPostBack) lblOut.Visible = !lblOut.Visible; }
</script> <html> <head> </head> <body>
<form runat="server"> Click Submit several times <asp:Button id="Button1" runat="server" Text="Submit"></asp:Button> <asp:Label id="lblOut" runat="server" text="See Me"></asp:Label> </form>
</body> </html>
</source>
BorderWidth, BorderColor, Font-Size, Font-Name, ForeColor, BackColor , ToolTip, Visible for asp:Label (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
lbl1.BorderStyle = 9 lbl1.Text = "Sample Border Style: " _ & 9
End Sub Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
lbl1.BorderStyle = txtBorderStyle.Text lbl1.Text = "Sample Border Style: " _ & txtBorderStyle.Text
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Label Controls Sample Page</TITLE> </HEAD> <BODY > <form runat="server"> <asp:Label
id="lbl1" BorderWidth="7px" BorderColor="Blue" Width="90%" Font-Size="25pt" Font-Name="Arial" runat="server"
/> <asp:Label
id="lbl2" Font-Size="12pt" Font-Bold="True" Font-Name="Lucida Console" ForeColor="DarkRed" BackColor="Silver" Text="
Enter a Border Style" ToolTip="Number must be between 0 and 9" runat="server"
/>
<asp:Label
id="lbl3" Text="Will not see me." Visible="False" runat="server"
/> <asp:TextBox
id="txtBorderStyle" Columns="25" MaxLength="30" runat=server
/>
<asp:Button
id="butOK" text="OK" Type="Submit" OnClick="SubmitBtn_Click" runat="server"
/> </Form> </BODY> </HTML>
</source>
Change asp label text in asp button action (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
protected void btnAdd_Click(object sender, EventArgs e) { Label1.Text = (Int32.Parse(Label1.Text) + 1).ToString(); Label2.Text = (Int32.Parse(Label2.Text) + 1).ToString(); }
</script> <html> <head>
<title>Disable View State</title>
</head> <body>
<form id="form1" runat="server">
Label 1: <asp:Label id="Label1" EnableViewState="false" Text="0" Runat="server" /> Label 2: <asp:Label id="Label2" Text="0" Runat="server" /> <asp:Button id="btnAdd" Text="Add" OnClick="btnAdd_Click" Runat="server" />
</form>
</body> </html>
</source>
Change Label to be visible (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
protected void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Visible = true; }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Simple Page</title>
</head> <body>
<form id="form1" runat="server">
<asp:Button id="btnSubmit" Text="Click Here!" OnClick="btnSubmit_Click" Runat="server" />
<asp:Label id="lblMessage" Text="Thank You!" Visible="false" Runat="server" />
</form>
</body> </html>
</source>
Define function to change Label Font to Italic (C#)
<source lang="csharp">
<%@ Page Language="C#" Debug="true" %> <script runat="server">
void Page_Load() { MakeItalic(Label1, CheckBox1.Checked); MakeItalic(Label2, CheckBox2.Checked); MakeItalic(Label3, CheckBox3.Checked); } void MakeItalic(Label TargetLabel, bool ItalicYN) { TargetLabel.Font.Italic = ItalicYN; }
</script> <html> <head>
<title>Parameter Web Controls</title>
</head> <body>
<form runat="server">
<asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox> |
<asp:CheckBox id="CheckBox2" runat="server"></asp:CheckBox> |
<asp:CheckBox id="CheckBox3" runat="server"></asp:CheckBox> |
<asp:Label id="Label1" runat="server" text="Label1"></asp:Label> | <asp:Label id="Label2" runat="server" text="Label2"></asp:Label> | <asp:Label id="Label3" runat="server" text="Label3"></asp:Label> |
<asp:Button id="Button1" runat="server" Text="Change Font Style"></asp:Button> </form>
</body> </html>
</source>
Display all page event in asp label (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
void Page_Load(object sender, EventArgs e) { Label1.Text = "Page Load"; } void Button1_Click(object sender, EventArgs e) { Label1.Text += "
Button Click"; } void Page_PreRender() { Label1.Text += "
Page PreRender"; }
</script> <html> <head>
<title>Show Page Events</title>
</head> <body>
<form id="form1" runat="server">
<asp:Button id="Button1" Text="Click Here" OnClick="Button1_Click" Runat="server" /> <asp:Label id="Label1" Runat="server" />
</form>
</body> </html>
</source>
Do a simple calculation and set value to asp:Label (VB.net)
<source lang="csharp">
<script language="vb" runat="server"> Sub Page_Load()
Dim intEarn As Integer Dim intTax As Integer Dim decTotal As Decimal intEarn = 150 intTax = 23 decTotal = intEarn - ((intEarn/100)*intTax) Display1.Text = decTotal
End Sub </script> <html> <head>
<title>Declaring Variables</title>
</head> <body>
Your total earnings after tax are $ <asp:label id="Display1" runat="server" />
</body> </html>
</source>
Label"s color
<source lang="csharp">
<%@ Page Language="vb" %> <html>
<head> <title>Init event example</title> <script runat="server"> Dim TheColor As System.Drawing.Color Sub Page_Init() TheColor = System.Drawing.Color.Red End Sub Sub Page_Load() Message.ForeColor = TheColor Message.Text = "The color of this text was set in Page_Init." TheColor = System.Drawing.Color.Blue Message2.ForeColor = TheColor Message2.Text = "The color of this text was set in Page_Load." End Sub </script> </head>
<body>
<asp:label id="Message" runat="server"/>
<asp:label id="Message2" runat="server"/>
</body> </html>
</source>
Set Asp Label Text during page load (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
void Page_Load() { Message.Text="The ASP.NET line"; }
</script> <html> <head>
<title>Inserting ASP.NET code Example</title>
</head> <body>
<asp:Label id="Message" runat="server"></asp:Label>
</body> </html>
</source>
Set asp:Label text (VB.net)
<source lang="csharp">
<script language="VB" runat="server"> Sub Page_Load()
Message.Text="The ASP.NET line"
End Sub
</script>
<html>
<head>
<title>Inserting ASP.NET code Example</TITLE>
</head>
<body>
First HTML Line
<asp:label id=Message runat="server"/>
Second HTML Line
</body>
</html>
</source>
Set color and font for an asp:Label (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Dim ColorList(6) as String Dim FontList as new ArrayList() Sub Page_Load ColorList(0) = "Red" ColorList(1) = "Orange" ColorList(2) = "Yellow" ColorList(3) = "Green" ColorList(4) = "Blue" ColorList(5) = "Indigo" ColorList(6) = "Violet" FontList.Add("Times New Roman") FontList.Add("Arial") FontList.Add("Verdana") FontList.Add("Comic Sans MS") If Not Page.IsPostback Dim ColorName as String For Each ColorName in ColorList ddlColorList.Items.Add(ColorName) Next ddlFontList.DataSource = FontList ddlFontList.DataBind() End If End Sub Sub btnSelectColor_Click(sender As Object, e As EventArgs) lblOutputMessage.Text = "You selected " & _ ddlColorList.SelectedItem.Value & " text written in " & _ ddlFontList.SelectedItem.Value lblOutputMessage.ForeColor = _ System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text) lblOutputMessage.Font.Name = _ ddlFontList.SelectedItem.Text End Sub
</script> <html> <head> </head> <body>
<form runat="server">
Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>
Then select a font sytle from the list: <asp:DropDownList id="ddlFontList" runat="server"></asp:DropDownList>
<asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>
<asp:Label id="lblOutputMessage" runat="server"></asp:Label>
</form>
</body> </html>
</source>
Set Label background color (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
void page_Load() { if(Page.IsPostBack) { if(chkIsMember.Checked==true){ lblOut.Text = "Members get a free ticket"; lblOut.BackColor=System.Drawing.Color.LightPink; } else if(Convert.ToInt32(txtAge.Text)<=18) { lblOut.Text = "Students get a free ticket"; lblOut.BackColor=System.Drawing.Color.LightPink; }else { lblOut.Text = "Price is 500"; lblOut.BackColor=System.Drawing.Color.LightSeaGreen; } } }
</script> <html> <head> </head> <body>
<form runat="server"> Please enter your age <asp:TextBox id="txtAge" runat="server"></asp:TextBox>
Are you a member? <asp:CheckBox id="chkIsMember" runat="server"></asp:CheckBox> <asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
<asp:Label id="lblOut" runat="server"></asp:Label>
</form>
</body> </html>
</source>
Set Label style (VB.net)
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<title>Control Properties Example</title> <script runat="server"> Sub Page_Load() Label2.Style("background-color") = "silver" End Sub </script> <style> .Hello { font: 14pt arial; color:blue; } </style>
</head> <body>
Control Properties Example
<form runat="server"> <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server"> <asp:tablerow runat="server"> <asp:tablecell runat="server"> HtmlInputText Control: </asp:tablecell> <asp:tablecell runat="server"> <input id="Text1" type="text" style="font: 12pt arial; background-color:silver; color:red;" runat="server"/> </asp:tablecell> </asp:tablerow> <asp:tablerow runat="server"> <asp:tablecell runat="server"> Label with Style: </asp:tablecell> <asp:tablecell runat="server"> <asp:label id="Label2" cssclass="Hello" runat="server"> Hello, World! </asp:label> </asp:tablecell> </asp:tablerow> </asp:table> </form>
</body> </html>
</source>
Set static asp:Label value
<source lang="csharp">
<html> <body>
<asp:label id="Message1" runat="server" text="Chris"/> you have selected to receive information about <asp:label id="Message2" runat="server" text="Oslo"/>
The information pack will be sent out within the next 24 hours. </body> </html>
</source>
Your First Page: display a Text runat Server (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
lblMessage.Text = "www.nfex.ru"
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Hello Web Sample Page</TITLE> </HEAD> <BODY>
<form runat="server"> <asp:Label id="lblTitle" Width="90%" Font-Size="25pt" Font-Name="Arial" Text="Test Page" runat="server" /> <asp:Label id="lblMessage" runat="Server" Font-Bold="True" />
</Form> </BODY> </HTML>
</source>