ASP.Net/Asp Control/Label
Содержание
- 1 asp:Button action to change the asp:Label and generate random number (VB.net)
- 2 asp:Label background color (VB.net)
- 3 asp:label: border, font and background color
- 4 Asp server Label: display server time (C#)
- 5 Assign current time to asp label (C#)
- 6 Boolean value toggle: label Visible (C#)
- 7 BorderWidth, BorderColor, Font-Size, Font-Name, ForeColor, BackColor , ToolTip, Visible for asp:Label (VB.net)
- 8 Change asp label text in asp button action (C#)
- 9 Change Label to be visible (C#)
- 10 Define function to change Label Font to Italic (C#)
- 11 Display all page event in asp label (C#)
- 12 Do a simple calculation and set value to asp:Label (VB.net)
- 13 Label"s color
- 14 Set Asp Label Text during page load (C#)
- 15 Set asp:Label text (VB.net)
- 16 Set color and font for an asp:Label (VB.net)
- 17 Set Label background color (C#)
- 18 Set Label style (VB.net)
- 19 Set static asp:Label value
- 20 Your First Page: display a Text runat Server (VB.net)
asp:Button action to change the asp:Label and generate random number (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
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">
<h1>Snake Eyes!
</h1>
<p>
Keep going, but don"t get two 1"s...
</p>
<p>
<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>
</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-Names="Verdana" Font-Size="X-Large" ForeColor="Red">0</asp:Label>
</p>
<p>
Last Score:<asp:Label id="Label5" runat="server" Font-Names="Verdana" Font-Size="X-Large" ForeColor="Red">0</asp:Label>
</p>
<p>
High Score:
<asp:Label id="Label6" runat="server" Font-Names="Verdana" Font-Size="X-Large" ForeColor="Red">0</asp:Label>
</p>
<p>
<asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Reset High Scores"></asp:Button>
</p>
</form>
</body>
</html>
asp:Label background color (VB.net)
<%@ 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>
<h1>Control Properties Example</h1>
<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>
asp:label: border, font and background color
<%@ 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="<CENTER>Welcome to<BR>Our Site</CENTER>"
runat="server"
/>
<BR><BR>
<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"
/>
<BR><BR>
<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"
/>
<BR><BR>
<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>
Asp server Label: display server time (C#)
<%@ 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>
Assign current time to asp label (C#)
<%@ 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">
<div>
Welcome to ASP.NET 2.0! The current date and time is:
<asp:Label
id="lblServerTime"
Runat="server" />
</div>
</form>
</body>
</html>
Boolean value toggle: label Visible (C#)
<%@ 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>
BorderWidth, BorderColor, Font-Size, Font-Name, ForeColor, BackColor , ToolTip, Visible for asp:Label (VB.net)
<%@ 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">
<Font Face="Tahoma" Size="+1">
<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="<BR><BR>Enter a Border Style"
ToolTip="Number must be between 0 and 9"
runat="server"
/>
<BR><BR>
<asp:Label
id="lbl3"
Text="Will not see me."
Visible="False"
runat="server"
/>
<asp:TextBox
id="txtBorderStyle"
Columns="25"
MaxLength="30"
runat=server
/>
<BR><BR>
<asp:Button
id="butOK"
text="OK"
Type="Submit"
OnClick="SubmitBtn_Click"
runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>
Change asp label text in asp button action (C#)
<%@ 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">
<div>
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" />
</div>
</form>
</body>
</html>
Change Label to be visible (C#)
<%@ 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">
<div>
<asp:Button
id="btnSubmit"
Text="Click Here!"
OnClick="btnSubmit_Click"
Runat="server" />
<br /><br />
<asp:Label
id="lblMessage"
Text="Thank You!"
Visible="false"
Runat="server" />
</div>
</form>
</body>
</html>
Define function to change Label Font to Italic (C#)
<%@ 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">
<table>
<tbody>
<tr>
<td>
<asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox>
</td>
<td>
<asp:CheckBox id="CheckBox2" runat="server"></asp:CheckBox>
</td>
<td>
<asp:CheckBox id="CheckBox3" runat="server"></asp:CheckBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="Label1" runat="server" text="Label1"></asp:Label></td>
<td>
<asp:Label id="Label2" runat="server" text="Label2"></asp:Label></td>
<td>
<asp:Label id="Label3" runat="server" text="Label3"></asp:Label></td>
</tr>
</tbody>
</table>
<asp:Button id="Button1" runat="server" Text="Change Font Style"></asp:Button>
</form>
</body>
</html>
Display all page event in asp label (C#)
<%@ 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 += "<br />Button Click";
}
void Page_PreRender()
{
Label1.Text += "<br />Page PreRender";
}
</script>
<html>
<head>
<title>Show Page Events</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button
id="Button1"
Text="Click Here"
OnClick="Button1_Click"
Runat="server" />
<asp:Label
id="Label1"
Runat="server" />
</div>
</form>
</body>
</html>
Do a simple calculation and set value to asp:Label (VB.net)
<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>
Label"s color
<%@ 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"/>
<br/>
<asp:label id="Message2" runat="server"/>
</body>
</html>
Set Asp Label Text during page load (C#)
<%@ 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>
<br />
</body>
</html>
Set asp:Label text (VB.net)
<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<br/>
<asp:label id=Message runat="server"/> <br />
Second HTML Line<br/>
</body>
</html>
Set color and font for an asp:Label (VB.net)
<%@ 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">
<p>
Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>
</p>
<p>
Then select a font sytle from the list:
<asp:DropDownList id="ddlFontList" runat="server"></asp:DropDownList>
</p>
<p>
<asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>
</p>
<p>
<asp:Label id="lblOutputMessage" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
Set Label background color (C#)
<%@ 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>
<br />
<form runat="server">
Please enter your age
<asp:TextBox id="txtAge" runat="server"></asp:TextBox>
<br />
Are you a member?
<asp:CheckBox id="chkIsMember" runat="server"></asp:CheckBox>
<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
<br />
<asp:Label id="lblOut" runat="server"></asp:Label>
<br />
</form>
</body>
</html>
Set Label style (VB.net)
<%@ 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>
<h1>Control Properties Example</h1>
<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>
Set static asp:Label value
<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>
Your First Page: display a Text runat Server (VB.net)
<%@ 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">
<Font Face="Tahoma">
<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"
/>
</Font>
</Form>
</BODY>
</HTML>