Page Level Variable (C#) : Variable Scope « Language Basics « ASP.Net






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>
           
       








Related examples in the same category

1.Local variable inside a function (VB.net)
2.Page level variable and function level variable (VB.net)
3.Define page level variables (VB.net)
4.Function Level variables (C#)
5.block-level variables (C#)