ASP.NET Webフォーム 変数の取り扱い方法

※~.aspx.cs
public partial class MyForm : System.Web.UI.Page
{

 // モジュール変数
 protected const string CONST_myValue = @”サーバー変数1″;
 protected string mMyStr = @”サーバー変数2″;

 protected void Page_Load(object sender, EventArgs e)
 {
  // セッション変数
  Page.Session[“value”] = “セッション変数”;
  ※「ASP.NET Webフォーム 組み込みオブジェクト」参照
 }
}

※~.aspx
<%@ Page
 Language=”C#”
 AutoEventWireup=”true”
 CodeBehind=”~.aspx.cs”
 Inherits=”~.~” %>
 ※「ASP.NET Webフォーム Pageディレクティブ」参照

※ASPXファイル上でC#/VBの資源を扱う場合は<%~%>で囲む
<%
 // ローカル変数
 string myValue = “ローカル変数”; 
%>
<%=myValue %>
→ ローカル変数
<%=CONST_myValue %>
→ サーバー変数1
<%=Session[“value”] %>
→ セッション変数

埋め込み
<asp:Button ID=”MyButton”
 runat=”server” Text=“<%=myValue %>”>
<script type=”text/javascript”>
 var str = “<%=this.mMyStr%>“;
 → サーバー変数2
</script>