我正在尝试从母版页访问服务器端变量,并不断出现“ xxx在当前上下文中不存在”错误,并且似乎无法找出原因。
这是我定义变量的方式以及尝试访问它们的方式:
namespace ABC
{
public partial class SiteMaster : MasterPage
{
public string Version { get { return Global.VersionInfo; } }
public string Active_server { get { return Global.ipaddress; } }
public string Environment_Value { get { return Global.Apppliaction_Environment; } }
public string Is_Authenticated { get { return Session["IsAuthenticated"].ToString(); } }
public string User_Role { get { return Session["UserRole"].ToString(); } }
public string User_Name { get { return Session["AceId"].ToString(); } }
public string Session_ID { get { return Session["SessionID"].ToString(); } }
protected void Page_Load(object sender, EventArgs e)
...
}
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="ABC.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
...
<script type="text/javascript">
//Access Code-Behind Data in Javascript
var Role = '<%=User_Role%>';
var UserName = '<%=User_Name%>';
var VersionValue = '<%=Version%>';
var EnvironmentValue = '<%=Environment_Value%>';
var IsAuthenticated = '<%=Is_Authenticated %>';
var Session_ID = '<%=Session_ID %>';
</script>
</head>