カスタムエラーページの表示 [ASP.NET]
ASP.NETアプリケーションで、サーバーエラーが発生した際に、
IISデフォルトのエラー表示ではなく、エラーページを独自のエラーページに飛ばす為の
web.configの設定。
<customErrors defaultRedirect="error.html" mode="On" />
</system.web>
※独自にerror.htmlというページを用意しておく
動的にページタイトルを変える [ASP.NET]
ASP.NET2.0よりPageのHeaderプロパティを使って、titleタグ内を動的に変更させる事ができる。
protected void Button1_Click(object sender, EventArgs e)
{
//ボタンが押されたタイミングでタイトルを変える
Page.Header.Title = "ボタンが押されました-" + DateTime.Now.ToString();
}
またHeaderプロパティを使わずに、タイトルを動的に変更する方法
(略)
<html xmlns="http:⁄⁄www.w3.org/1999/xhtml" >
<head runat="server">
<title><%=strTitle %></title>
</head>
(略)
public partial class changetitle : System.Web.UI.Page
{
protected string strTitle = "";
protected void Page_Load(object sender, EventArgs e)
{
strTitle = "ページロード -" + DateTime.Now.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
strTitle = "ボタンが押されました -" + DateTime.Now.ToString();
}
}
フレームを指定してRedirectをする [ASP.NET]
関連記事:フレームにRedirectを擬似的に行う
前回のJavaScriptを使った擬似的なRedirectに近いのですが、
今回はボタンコントロール(等)にtargetを指定して、指定したフレームをページ遷移させます。
protected void Page_Load(object sender, EventArgs e)
{
//frameAというフレームを指定する
Button1.Attributes["OnClick"] = "target='frameA';";
//親フレームを指定する
Button2.Attributes["OnClick"] = "target='_top';";
}
protected void Button1_Click(object sender, EventArgs e)
{
//frameAに対して、frameA.aspxというページに移動させる
Response.Redirect("frameA.aspx");
Response.End();
}
protected void Button2_Click(object sender, EventArgs e)
{
//親フレームをtop.aspxというページに移動させる
Response.Redirect("top.aspx");
Response.End();
}
フレームにRedirectを擬似的に行う [ASP.NET]
ASP.NETのResponse.Redirect,Server.Transferでは
フレームを指定してのページ移動はできない為、JavaScriptを使ってページ遷移をする。
以下はframeAという名前のついたフレームに対して、ページ遷移を実行します。
string strScriptName = "RedirectScript";
string strRedirectUrl= "redirect.aspx";
Type type = this.GetType();
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(type,strScriptName))
{
string strScriptText = "parent.frameA.location.href='" + strRedirectUrl + "';";
cs.RegisterStartupScript(type,strScriptName, strScriptText, true);
}
ASP.NETでCSV形式のファイルをダウンロードさせる [ASP.NET]
関連記事:ASP.NETでJSファイルを生成する
ASP.NETで動的にCSVファイルを生成して、それをダウンロードさせる方法。
ContentTypeをapplication/octet-streamにして、ファイル名をjsの時と同じく新たに定義する。
protected void Page_Load(object sender, EventArgs e)
{
// ダウンロード用ファイルの名前を設定
Response.AddHeader("Content-Disposition",
"attachment;filename=test.csv");
//ダウンロードデータとして設定
Response.ContentType = "application/octet-stream";
//エンコードの指定
System.Text.Encoding enc =
System.Text.Encoding.GetEncoding("Shift-JIS");
string strOutdata = "";
strOutdata += "れうるーら,おはよう,こんにちは,こんばんは,おやすみ\r\n";
strOutdata += "さだらーん,ぐっどもーにんぐ,はろー,ぐっといぶにんぐ,ぐっとないと\r\n";
strOutdata += "ぐわじん,hi!!,hello!!,hahaha!!,zzz...\r\n";
//バイナリーで出力
Response.BinaryWrite(enc.GetBytes(strOutdata));
Response.End();
}
ページ読み込み完了後に処理を行う [ASP.NET]
.NET Framework2.0より、Page.LoadCompleteイベントが追加され、
ページの読み込みが完了した後にLoadCompleteイベントが発生する。
ページの読み込みが完了した後に何か処理をさせたい場合は、Page_LoadComplete内に記述。
{
//ページロード処理
}
//ページ読み込み完了後の処理
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write("LoadComplete");
}
}
ASP.NETでJSファイルを生成する [ASP.NET]
ASP.NETで、ContentTypeをapplication/x-javascriptにして出力し、
動的にJSファイルを生成する方法。
<html>
<head>
<title>サンプル</title>
<script type="text/javascript" src="sample.aspx?q=abc"></script>
</head>
<body>
<script type="text/javascript">funcOutput();</script>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
//ここで出力用のファイル名を定義する。ここではsample.jsというファイル名で出力する
Response.AddHeader("Content-Disposition","attachment;filename=sample.js");
//ContentTypeをapplication/x-javascriptに設定する
Response.ContentType = "application/x-javascript";
//JavaScriptのコードを記入。
//この辺をホントはもっと動的に生成
string strJs = "function funcOutput(){";
strJs += "alert('" + Request.QueryString["q"] + "');";
strJs += "}";
Response.Write(strJs);
Response.End();
}
ボタンクリック時にイベント処理を行う前に確認ダイアログを出す [ASP.NET]
.NET Framework2.0でボタンコントロールが押された時に、
onClickで設定されたイベントを行う前に、確認ダイアログを出す方法。
ButtonのOnClientClickプロパティに、確認ダイアログを出すJavaScriptを記述する。
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ボタン" OnClientClick="return confirm('確認:よろしいですか?');" />
//2回確認を表示させる。
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ジャンプ"
OnClientClick="if(confirm('移動してもよろしいですか?')){return confirm('最終確認:本当に移動してもよろしいですか?');}else{return false;}"
/>
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http:⁄⁄www.google.co.jp/");
}
コードからJavaScriptを呼び出す [ASP.NET]
HTML内にJavaScriptのスクリプトを記述するのではなく、
コード内にJavaScriptを記述し、呼び出す方法。
ClientScriptManager.RegisterStartupScript
(this.GetType(),"登録するスクリプトの名前",
"スクリプト","スクリプトタグを追加するかどうかのbool値");
string strScriptName = "PopupScript";
Type type = this.GetType();
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(type,strScriptName))
{
string strScriptText = "alert('こんばんは。');";
cs.RegisterStartupScript(type,strScriptName, strScriptText, true);
}
分離コード モジュールでServer.MapPathを使う [ASP.NET]
ASP.NETでWebサーバー上の仮想パスに対応する物理パスを取得する場合は、
Server.MapPath("仮想パス");で事足りるが、
自作クラスファイル内ではServer.MapPath("仮想パス");を使う事ができない。
その場合は、
HttpContext.Current.Server.MapPath("仮想パス");
を使用する。
string strFilePath;
strFilePath = HttpContext.Current.Server.MapPath("仮想パス");