#includeを利用して、aspxファイル内に別のファイルを埋め込むコードを紹介します。
概要
ASP.NET ではaspxファイル内に別のファイルを埋め込むことができます。
別のファイルを埋め込む場合には #incldueを用います。aspxファイル内に
次のコードを記述することで、aspxファイルを表示した際に指定したファイルの内容を(以下の場合はanalytics.inc)を#includeを記述した位置に埋め込むことができます。
<!-- #include file ="analytics.inc" -->
例
別ファイルで用意したGoogleAnalyticsのコードを#includeを使い埋め込む例です。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="iPentecDocument._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<!-- #include file ="analytics.inc" -->
<title>無題のページ</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<!--Google Analytics-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'XX-XXXXXXXX']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol
? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ga);
})();
</script>
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-06-11
作成日: 2010-08-09