LoginSignup
31
32

More than 5 years have passed since last update.

インライン式(<%=, <%@, <%#, <%$, <%--)の概要

Posted at

.NET Framework の ASP.NET インライン式の概要から

< %... % >  … コードブロックを埋め込む

<%@ Page Language="VB" %>
<html>
<body>
    <form id="form1" runat="server">
    <% For i As Integer = 16 To 24 Step 2%>
    <div style="font-size: <% Response.Write(i)%>">
        Hello World<br />
    </div>
    <% Next%>
    </form>
</body>
</html>

<%= ... %>  … 式を表示する

Response.Write(...) のステートメントと同等

<%@ Page Language="VB" %>
<html>
<body>
    <form id="form1" runat="server">
    <%=DateTime.Now.ToString() %>
    </form>
</body>
</html>

< % @... % > … ディレクティブの式

いろいろ種類がある

テキスト テンプレートのディレクティブの構文

Text Template Directive Syntax

< %#... % > … データバインディング式

コントロールのDataBindメソッドが呼び出されるとき、指定したプロパティの値に変換する

以下の場合、Submitボタンを押すと <%# ... %> が選択した文字に表示される

sample.aspx
    <script language="C#" runat="server"> 
        void SubmitBtn_Click(Object sender, EventArgs e) { 
          Page.DataBind(); 
        } 
    </script> 

    <form runat="server"> 
        <asp:DropDownList id="StateList" runat="server"> 
          <asp:ListItem>CA</asp:ListItem> 
          <asp:ListItem>IN</asp:ListItem> 
          <asp:ListItem>KS</asp:ListItem> 
          <asp:ListItem>MD</asp:ListItem> 
        </asp:DropDownList>        
        <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>         
        <p>
        <!-- CA,IN,...が表示される -->
        Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>      
    </form> 

データ バインディング式の構文

Data-Binding Expression Syntax

< % $... % > … 式ビルダー

次のファイルから参照する

  • アプリケーション構成ファイル(Web.config)
  • リソースファイル(*.resx)

構文

<% $ 式プレフィックス: 式の値 %>

式プレフィックスの例

式プレフィックス
AppSettings AppSettingsから参照
ConnectionStrings connectionStringsから参照
Resources リソースファイルから参照

上記以外も指定できるらしい

サンプル

AppSettingsサンプル

Web.config
<appSettings>
    <add key="copyright" value="(c) Copyright 2009 WebSiteName.com"/>
</appSettings>
sample.aspx
<div id="footer">
    <asp:Literal ID="Literal1" runat="server" Text="<%$ AppSettings: copyright %>"></asp:Literal>
</div>
ConnectionStringsサンプル

Web.config
<configuration>
  <connectionStrings>
    <add name="NorthwindConnectionString1" 
      connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
sample.aspx
<asp:SqlDataSource ID="SqlDataSource1" Runat="server" 
    SelectCommand="SELECT * FROM [Employees]"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>">
</asp:SqlDataSource>

引用元はConnectionStringsの大文字小文字が区別されていない

Resourcesサンプル

Messages.resx
<data name="ThankYouLabel"><value>Thank you very much!</value></data>
sample.aspx
<asp:Label id="label1" runat="server" text="<%$ Resources: Messages, ThankYouLabel %>" />

リソースファイル名を指定する

ASP.NET Expressions Overview

ASP.NET 式の概要

< %--...--% >  … サーバー側コメントブロック

サーバー側コメントブロック
クライアントには送られない

<%@ Page Language="VB" %>
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim strName As String
        strName = Session("userName")
        lblUserName.Text = strName
    End Sub
</script>
<html>
<body>
    <form id="form1" runat="server">
    <%-- Label for UserName このコメント行はクライアントへは送られない --%>
    <asp:Label ID="lblUserName" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>
31
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
32