- 追加された行はこの色です。
- 削除された行はこの色です。
#freeze
閲覧総計:&counter(); (本日:&counter(today); 昨日:&counter(yesterday);)
一度入力したデータを自分のPCに保存し、次回起動時に以前入力したデータを
利用できる「クッキー機能」の基本的な操作法を確認した。 09/08/10
(ポイント)
ASP.NET標準のクッキー機能だけだと、日本語が文字化けしてしまう模様。
対策定石としてUrlエンコード、デコードを使うそうな。
開発環境:VWD2005+AJAX1.0+Toolkit + SQL Server2005
サーバ:ASP.NET2.0+AJAX1.0 + SQL Server2005
【機能】TextBoxに入力した値が、次回起動した時に表示される。
【稼動サンプル】
http://www.kuri6005.useiis7.net/4AspNet10/Cookie/Cookie.aspx
最初に起動した時には、何も表示されない。▼
#ref(cookie01.JPG);
次に起動した時には、前回入力したデータが表示される。▼
#ref(cookie02.JPG);
【Cookie.aspx】
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
'Cookie操作法で参考にしたページ 08/10/26
'http://momotchi.net/forums/t/23.aspx
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'最初のPage_Loadの時だけ実行する処理。すなわちPostBuck時は実行しない。
If Not Page.IsPostBack Then
'Cookie情報の読込
Dim cookie As HttpCookie = Request.Cookies("UserInfo")
'Cookie情報がある場合
If Not (cookie Is Nothing) Then
'日本語が文字化けしないようUrlDecodeを利用 06/02/27
'http://social.msdn.microsoft.com/Forums/ja-JP/vsfeedbackja/thread/6ea71b80-54cf-4770-bbf5-0ea764c02f6d
TextBox1.Text = Server.UrlDecode(cookie.Values("name"))
TextBox2.Text = Server.UrlDecode(cookie.Values("password"))
End If
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'Cookie情報の書込(更新)
Dim cookie As New HttpCookie("UserInfo")
'日本語が文字化けしないようUrlEncodeを利用 06/02/27
'http://social.msdn.microsoft.com/Forums/ja-JP/vsfeedbackja/thread/6ea71b80-54cf-4770-bbf5-0ea764c02f6d
cookie.Values("name") = Server.UrlEncode(TextBox1.Text)
cookie.Values("password") = Server.UrlEncode(TextBox2.Text)
cookie.Expires = DateTime.Now.AddMonths(3)
Response.Cookies.Add(cookie)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>クッキー操作の基本</title>
</head>
<body>
<form id="form1" runat="server">
<div>
クッキー操作の基本 09/08/10<br />
<br />
【機能】TextBoxに入力した値が、次回起動した時に表示される。<br />
名前:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
パスワード:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="クッキーの更新" OnClick="Button1_Click" /><br />
</div>
</form>
</body>
</html>
【参考にしたページ】
1.Cookie操作法で参考にしたページ 08/10/26
http://momotchi.net/forums/t/23.aspx
2.日本語が文字化けしないようUrlEncodeを利用 06/02/27
http://social.msdn.microsoft.com/Forums/ja-JP/vsfeedbackja/thread/6ea71b80-54cf-4770-bbf5-0ea764c02f6d
#vote(参考になった[3],ふつう[0],参考にならなかった[0])
- 1 -- [[1]] &new{2013-10-26 (土) 21:29:45};
- 1 -- [[1]] &new{2013-10-26 (土) 21:29:56};
- 1 -- [[-1']] &new{2013-10-26 (土) 21:30:03};
-- 1' -- [[1]] &new{2013-10-26 (土) 21:30:07};
- 1 -- [[1]] &new{2013-10-26 (土) 21:30:13};
- 1 -- [[1]] &new{2014-01-16 (木) 21:03:04};
- 1 -- [[1]] &new{2014-01-16 (木) 21:03:12};
- 1 -- [[-1']] &new{2014-01-16 (木) 21:03:15};
-- 1' -- [[1]] &new{2014-01-16 (木) 21:03:19};
- 1 -- [[1]] &new{2014-01-16 (木) 21:03:25};
#comment_nospam
- 1 -- [[1]] &new{2014-01-16 (木) 21:03:08};
- 1 -- [[1]] &new{2013-10-26 (土) 21:29:50};