WebForm1.aspx.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:8k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. namespace PCSWebApp3
  12. {
  13. /// <summary>
  14. /// Summary description for WebForm1.
  15. /// </summary>
  16. public class WebForm1 : System.Web.UI.Page
  17. {
  18.       protected System.Web.UI.WebControls.TextBox nameBox;
  19.       protected System.Web.UI.WebControls.RequiredFieldValidator validateName;
  20.       protected System.Web.UI.WebControls.Calendar calendar;
  21.       protected System.Web.UI.WebControls.TextBox eventBox;
  22.       protected System.Web.UI.WebControls.RequiredFieldValidator validateEvent;
  23.       protected System.Web.UI.WebControls.DropDownList roomList;
  24.       protected System.Web.UI.WebControls.RequiredFieldValidator validateRoom;
  25.       protected System.Web.UI.WebControls.ListBox attendeeList;
  26.       protected System.Web.UI.WebControls.RequiredFieldValidator validateAttendees;
  27.       protected System.Web.UI.WebControls.Button submitButton;
  28.       protected System.Web.UI.WebControls.ValidationSummary validationSummary;
  29.       protected System.Web.UI.WebControls.Label resultLabel;
  30.       protected System.Data.OleDb.OleDbConnection oleDbConnection1;
  31.       protected System.Data.DataSet ds;
  32.       protected System.Data.DataTable eventTable;
  33.       protected System.Data.OleDb.OleDbDataAdapter daAttendees;
  34.       protected System.Data.OleDb.OleDbDataAdapter daRooms;
  35.       protected System.Web.UI.WebControls.DataGrid eventDetails1;
  36.       protected System.Web.UI.WebControls.DataList eventDetails2;
  37.       protected System.Web.UI.WebControls.Label edName;
  38.       protected System.Web.UI.WebControls.Label edDate;
  39.       protected System.Web.UI.WebControls.Label edRoom;
  40.       protected System.Web.UI.WebControls.Label edAttendees;
  41.       protected System.Data.OleDb.OleDbDataAdapter daEvents;
  42.       private void Page_Load(object sender, System.EventArgs e)
  43.       {
  44.          oleDbConnection1.Open();
  45.          ds = new DataSet();
  46.          daAttendees = new System.Data.OleDb.OleDbDataAdapter(
  47.             "SELECT * FROM Attendees",
  48.             oleDbConnection1);
  49.          daRooms = new System.Data.OleDb.OleDbDataAdapter(
  50.             "SELECT * FROM Rooms", oleDbConnection1);
  51.          daEvents = new System.Data.OleDb.OleDbDataAdapter(
  52.             "SELECT * FROM Events", oleDbConnection1);
  53.          daAttendees.Fill(ds, "Attendees");
  54.          daRooms.Fill(ds, "Rooms");
  55.          daEvents.Fill(ds, "Events");
  56.          attendeeList.DataSource = ds.Tables["Attendees"];
  57.          roomList.DataSource = ds.Tables["Rooms"];
  58.          eventTable = ds.Tables["Events"];
  59.          eventDetails1.DataSource = eventTable;
  60.          eventDetails2.DataSource = eventTable;
  61.          if (!this.IsPostBack)
  62.          {
  63.             System.DateTime trialDate = System.DateTime.Now;
  64.             calendar.SelectedDate = getFreeDate(trialDate);
  65.             this.DataBind();
  66.          }
  67.          else
  68.          {
  69.             eventDetails1.DataBind();
  70.             eventDetails2.DataBind();
  71.          }
  72.          oleDbConnection1.Close();
  73.       }
  74. #region Web Form Designer generated code
  75. override protected void OnInit(EventArgs e)
  76. {
  77. //
  78. // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  79. //
  80. InitializeComponent();
  81. base.OnInit(e);
  82. }
  83. /// <summary>
  84. /// Required method for Designer support - do not modify
  85. /// the contents of this method with the code editor.
  86. /// </summary>
  87.       private void InitializeComponent()
  88.       {    
  89.          this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
  90.          // 
  91.          // oleDbConnection1
  92.          // 
  93.          this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=C:\Inetpub" +
  94.             "\wwwroot\PCSWebApp3\PCSWebApp3.mdb";
  95.          this.calendar.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.calendar_DayRender);
  96.          this.calendar.SelectionChanged += new System.EventHandler(this.calendar_SelectionChanged);
  97.          this.submitButton.Click += new System.EventHandler(this.submitButton_Click);
  98.          this.eventDetails2.SelectedIndexChanged += new System.EventHandler(this.eventDetails2_SelectedIndexChanged);
  99.          this.Load += new System.EventHandler(this.Page_Load);
  100.       }
  101. #endregion
  102.       private void submitButton_Click(object sender, System.EventArgs e)
  103.       {
  104.          if (this.IsValid)
  105.          {
  106.             String attendees = "";
  107.             foreach (ListItem attendee in attendeeList.Items)
  108.             {
  109.                if (attendee.Selected)
  110.                {
  111.                   attendees += attendee.Text + " (" + attendee.Value
  112.                      + "), ";
  113.                }
  114.             }
  115.             attendees += " and " + nameBox.Text;
  116.             String dateString =
  117.                calendar.SelectedDate.Date.Date.ToShortDateString();
  118.             String oleDbCommand = "INSERT INTO Events (Name, Room, " +
  119.                "AttendeeList, EventDate) VALUES ('" +
  120.                eventBox.Text + "', '" +
  121.                roomList.SelectedItem.Value + "', '" +
  122.                attendees + "', '" + dateString +
  123.                "')";
  124.             System.Data.OleDb.OleDbCommand insertCommand =
  125.                new System.Data.OleDb.OleDbCommand(oleDbCommand,
  126.                oleDbConnection1);
  127.             oleDbConnection1.Open();
  128.             int queryResult = insertCommand.ExecuteNonQuery();
  129.             if (queryResult == 1)
  130.             {
  131.                resultLabel.Text = "Event Added.";
  132.                daEvents = new System.Data.OleDb.OleDbDataAdapter(
  133.                   "SELECT * FROM Events", oleDbConnection1);
  134.                ds.Clear();
  135.                daEvents.Fill(ds, "Events");
  136.                eventTable = ds.Tables["Events"];
  137.                calendar.SelectedDate =
  138.                   getFreeDate(calendar.SelectedDate.AddDays(1));
  139.                eventDetails1.DataBind();
  140.                eventDetails2.DataBind();
  141.             }
  142.             else
  143.             {
  144.                resultLabel.Text = "Event not added due to DB access "
  145.                   + "problem.";
  146.             }
  147.             oleDbConnection1.Close();
  148.          }
  149.       }
  150.       private System.DateTime getFreeDate(System.DateTime trialDate)
  151.       {
  152.          if (eventTable.Rows.Count > 0)
  153.          {
  154.             System.DateTime testDate;
  155.             bool trialDateOK = false;
  156.             while (!trialDateOK)
  157.             {
  158.                trialDateOK = true;
  159.                foreach (System.Data.DataRow testRow in eventTable.Rows)
  160.                {
  161.                   testDate = (System.DateTime)testRow["EventDate"];
  162.                   if (testDate.Date == trialDate.Date)
  163.                   {
  164.                      trialDateOK = false;
  165.                      trialDate = trialDate.AddDays(1);
  166.                   }
  167.                }
  168.             }
  169.          }
  170.          return trialDate;
  171.       }
  172.       private void calendar_SelectionChanged(object sender, System.EventArgs e)
  173.       {
  174.          System.DateTime trialDate = calendar.SelectedDate;
  175.          calendar.SelectedDate = getFreeDate(trialDate);
  176.       }
  177.       private void calendar_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
  178.       {
  179.          if (eventTable.Rows.Count > 0)
  180.          {
  181.             System.DateTime testDate;
  182.             foreach (System.Data.DataRow testRow in eventTable.Rows)
  183.             {
  184.                testDate = (System.DateTime)testRow["EventDate"];
  185.                if (testDate.Date == e.Day.Date)
  186.                {
  187.                   e.Cell.BackColor = Color.Red;
  188.                }
  189.             } 
  190.          }
  191.       }
  192.       protected void eventDetails2_SelectedIndexChanged(object sender, System.EventArgs e)
  193.       {
  194.          eventDetails2.DataBind();
  195.          DataRow selectedEventRow =
  196.             eventTable.Rows[eventDetails2.SelectedIndex];
  197.          edName.Text = (string)selectedEventRow["Name"];
  198.          edDate.Text = "<b>Date:</b> " +
  199.             ((DateTime)selectedEventRow["EventDate"]).ToLongDateString();
  200.          edAttendees.Text = "<b>Attendees:</b> " +
  201.             (string)selectedEventRow["AttendeeList"];
  202.          DataRow selectedEventRoomRow =
  203.             ds.Tables["Rooms"].Rows[(int)selectedEventRow["Room"] - 1];
  204.          edRoom.Text = "<b>Room:</b> " + selectedEventRoomRow["Room"];
  205.       }
  206. }
  207. }