ASP.Net/Components/Guest Book

Материал из .Net Framework эксперт
Версия от 11:51, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Guest Book Demo (C#)

<%@ Page language="c#" src="GuestBook.aspx.cs" AutoEventWireup="false" Inherits="GuestBook.GuestBook" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <P>
        <asp:DataList id="GuestBookList" runat="server">
          <SelectedItemStyle></SelectedItemStyle>
          <ItemStyle></ItemStyle>
          <FooterStyle></FooterStyle>
          <ItemTemplate>
            Left By:
            <%# DataBinder.Eval(Container.DataItem, "Author") %>
            <br>
            <b>
              <%# DataBinder.Eval(Container.DataItem, "Message") %>
            </b>
            <br>
            Left On:
            <%# DataBinder.Eval(Container.DataItem, "Submitted") %>
          </ItemTemplate>
          <HeaderStyle></HeaderStyle>
        </asp:DataList>
        </P>
      <DIV style="WIDTH: 560px; POSITION: relative; HEIGHT: 248px" ms_positioning="GridLayout">
        <asp:Label id="Label1" runat="server" Width="104px" Height="24px">Your Name:</asp:Label>
        <asp:Label id="Label2" runat="server" Width="104px" Height="24px">Your Message:</asp:Label>
        <asp:TextBox id="txtName" runat="server" Width="392px"></asp:TextBox>
        <asp:TextBox id="txtMessage" runat="server" Width="392px" Height="154px" TextMode="MultiLine"></asp:TextBox>
        <asp:Button id="cmdSubmit" runat="server" Width="104px" Text="Submit"></asp:Button></DIV>
    </form>
  </body>
</HTML>

<%--
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace GuestBook
{
  /// <summary>
  /// Summary description for GuestBook.
  /// </summary>
  public class GuestBook : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.DataList GuestBookList;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.TextBox txtName;
    protected System.Web.UI.WebControls.TextBox txtMessage;
    protected System.Web.UI.WebControls.Button cmdSubmit;
      private string guestBookName;
    private void Page_Load(object sender, System.EventArgs e)
    {
      guestBookName = Server.MapPath("GuestBook");
      if (!this.IsPostBack)
      {
        GuestBookList.DataSource = GetAllEntries();
        GuestBookList.DataBind();
      }
    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
      this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    private void cmdSubmit_Click(object sender, System.EventArgs e)
    {
      BookEntry newEntry = new BookEntry();
      newEntry.Author = txtName.Text;
      newEntry.Submitted = DateTime.Now;
      newEntry.Message = txtMessage.Text;
      SaveEntry(newEntry);
      GuestBookList.DataSource = GetAllEntries();
      GuestBookList.DataBind();
      txtName.Text = "";
      txtMessage.Text = "";
    }
    private ArrayList GetAllEntries()
    {
      ArrayList entries = new ArrayList();
      DirectoryInfo guestBookDir = new DirectoryInfo(guestBookName);
      foreach (FileInfo fileItem in guestBookDir.GetFiles()){
        entries.Add(GetEntryFromFile(fileItem));
      }
      return entries;
    }
    private BookEntry GetEntryFromFile(FileInfo entryFile)
    {
      BookEntry newEntry = new BookEntry();
      StreamReader r = entryFile.OpenText();
      newEntry.Author = r.ReadLine();
      newEntry.Submitted = DateTime.Parse(r.ReadLine());
      newEntry.Message = r.ReadLine();
      r.Close();
      return newEntry;
    }
    private void SaveEntry(BookEntry entry)
    {
      string fileName = guestBookName + @"\";
      fileName += DateTime.Now.Ticks.ToString();
      FileInfo newFile = new FileInfo(fileName);
      StreamWriter w = newFile.CreateText();
      w.WriteLine(entry.Author);
      w.WriteLine(entry.Submitted.ToString());
      w.WriteLine(entry.Message);
      w.Flush();
      w.Close();
    }
  }

  public class BookEntry
  {
    private string author;
    private DateTime submitted;
    private string message;
    public string Author
    {
      get
      { return author; }
      set
      { author = value; }
    }
    public DateTime Submitted
    {
      get
      { return submitted; }
      set
      { submitted = value; }
    }
    public string Message
    {
      get
      { return message; }
      set
      { message = value; }
    }
  }
}

--%>