ASP.Net/Language Basics/Variable Scope
Содержание
block-level variables (C#)
<%@ Page Language="c#" %>
<script runat="server">
void Page_Load()
{
if(1==1)
{
string strBlockLevelVariable;
strBlockLevelVariable = "Very Short Lived!";
lblMessage.Text = strBlockLevelVariable;
}
}
</script>
<html>
<head>
<title>Creating Variables Example</title>
</head>
<body>
<asp:Label runat="server" ID="lblMessage"/><br/>
</body>
</html>
Define page level variables (VB.net)
<%@ Page Language="VB" %>
<script runat="server">
Dim myFirstNumber, mySecondNumber As Integer
Sub SetVarsToInitialValues()
myFirstNumber = 7
mySecondNumber = 8
Label1.Text = myFirstNumber
Label2.Text = mySecondNumber
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
SetVarsToInitialValues()
Label3.Text = myFirstNumber + mySecondNumber
End Sub
Sub Button2_Click(sender As Object, e As EventArgs)
SetVarsToInitialValues()
Label3.Text = myFirstNumber - mySecondNumber
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
+
<asp:Label id="Label2" runat="server">Label</asp:Label>
=
<asp:Label id="Label3" runat="server">Label</asp:Label>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Add"></asp:Button>
<asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Subtract"></asp:Button>
</p>
</form>
</body>
</html>
Function Level variables (C#)
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load()
{
string strMyFuncVariable = "Function-Level Variable";
if(Page.IsPostBack)
{
string strMyBlockVariableUsedInside = "Block Variable Used In Block";
lblMessageBlockInBlock.Text = strMyBlockVariableUsedInside;
lblMessageFunction.Text = strMyFuncVariable;
}
}
</script>
<html>
<head>
<title>Variable Scope</title>
</head>
<body>
<form runat="server">
<asp:Label id="lblMessageBlockInBlock" runat="server" text="DEFAULT - BlockInBlock"></asp:Label>
<br />
<asp:Label id="lblMessageFunction" runat="server" text="DEFAULT - Function"></asp:Label>
<br />
<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
</form>
</body>
</html>
Local variable inside a function (VB.net)
<script language="vb" runat="server">
Sub Page_Load()
Response.Write ("<BR />Calling Subroutine 1...")
Call Subroutine_1()
Response.Write ("<BR />Calling Subroutine 2...")
Call Subroutine_2()
Response.Write ("<BR />Calling Subroutine 1...")
Call Subroutine_1()
End Sub
Sub Subroutine_1()
Dim Different As String
Different = "<i>Hello I"m the variable Different in Subroutine 1</i>"
Response.Write (Different)
End Sub
Sub Subroutine_2()
Dim Different As String
Different = "<b>Hello I"m the variable Different in Subroutine 2</b>"
Response.Write (Different)
End Sub
</script>
<html>
<head>
<title>Scope</title>
</head>
<body>
</body>
</html>
Page level variable and function level variable (VB.net)
<script language="vb" runat="server">
Dim Global As String ="<br><u>Hello I"m a persistent global variable</u>"
Sub Page_Load()
Response.Write (Global)
Response.Write ("<BR />Calling Subroutine 1...")
Call Subroutine_1()
Response.Write ("<BR />Calling Subroutine 2...")
Call Subroutine_2()
Response.Write ("<BR />Calling Subroutine 1...")
Call Subroutine_1()
End Sub
Sub Subroutine_1()
Dim Different As String
Different = "<i>Hello I"m the variable Different in Subroutine 1</i>"
Response.Write (Different)
Response.Write (Global)
End Sub
Sub Subroutine_2()
Dim Different As String
Different = "<b>Hello I"m the variable Different in Subroutine 2</b>"
Response.Write (Different)
Response.Write (Global)
End Sub
</script>
<html>
<head>
<title>Scope</title>
</head>
<body>
</body>
</html>
Page Level Variable (C#)
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
string strVariableGlobal = "Variable Global: Use me anyplace on the page";
void Page_Load()
{
string strMyVariable1 = "Block Variable Used In Block";
if (1==1)
{
string strMyVariable2 = "Block Variable Used In Block";
string strMyVariable3 = "Block Variable Used After Block";
lblMessageBlockInBlock.Text = strVariableGlobal;
}
lblMessageProcedure.Text = strVariableGlobal;
SubProcedure2();
}
void SubProcedure2()
{
lblMessageBlockOutBlock.Text = strVariableGlobal;
}
</script>
<html>
<head>
<title>Variable Scope</title>
</head>
<body>
<form runat="server">
<asp:Label id="lblMessageBlockInBlock" runat="server" text="DEFAULT - BlockInBlock"/>
<br />
<asp:Label id="lblMessageBlockOutBlock" runat="server" text="DEFAULT - BlockOutBlock"></asp:Label>
<br />
<asp:Label id="lblMessageProcedure" runat="server" text="DEFAULT - Procedure"></asp:Label>
<br />
</form>
</body>
</html>