アプリ版:「スタンプのみでお礼する」機能のリリースについて

ASP.NETで
Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs)
TextBox1.Text = Calendar1.SelectedDate

カレンダーの日付をクリックすると簡単なコメントをTextBoxに表示
したい。

内容の呼び出しは
path = Server.Mappath("1.txt")
テキストファイルに
あああ
いいい
ううう
えええ
おおお
と改行されて保存してありこのファイルから呼び出したいです。

A 回答 (1件)

お世話になります。



そのテキストファイルのレイアウトだと
選択された日付とどのようにコメントを関連付けるのかが謎なので
以下のようなテキストファイルに変更すると仮定して書いてみました。
ちなみに、.NET Framework のバージョンが書いてありませんが
2.0 で記述してあります。

■テキストファイル(TextFile.txt)
2007/05/11,なんとかかんとか
2007/05/12,なんとか
2007/05/13,おやすみおやすみ

■ASPX(Default.aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitio …
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Test</title>
</head>
<body>
<form id="form1" runat="server">
<p><asp:Calendar ID="Calendar1" runat="server"></asp:Calendar></p>
<p><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></p>
</form>
</body>
</html>

■コードビハインド(Default.aspx.vb)
Partial Class _Default
  Inherits System.Web.UI.Page

  Private Const SESSION_KEY As String = "TextFileHashTable"
  Private m_stringsHashTable As Hashtable

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
      Me.m_stringsHashTable = New Hashtable()
      Dim textFilePath As String = System.IO.Path.Combine(Server.MapPath("."), "TextFile.txt")
      Using sr As System.IO.StreamReader = New System.IO.StreamReader(textFilePath, System.Text.Encoding.Default)
        While sr.Peek() > -1
          Dim readedLine As String = sr.ReadLine()
          Me.m_stringsHashTable.Add(readedLine.Split(","c)(0), readedLine.Split(","c)(1))
        End While
      End Using
      Session(SESSION_KEY) = Me.m_stringsHashTable
    Else
      Me.m_stringsHashTable = DirectCast(Session(SESSION_KEY), Hashtable)
    End If
  End Sub

  Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
    Dim comment As String = String.Empty
    Dim selectedDate As String = Me.Calendar1.SelectedDate.ToString("yyyy/MM/dd")
    If Me.m_stringsHashTable.ContainsKey(selectedDate) Then
      comment = DirectCast(Me.m_stringsHashTable(selectedDate), String)
    End If
    Me.TextBox1.Text = comment
  End Sub
End Class
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!