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

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. using PCSWebApp4.eventDataService;
  12. namespace PCSWebApp4
  13. {
  14. /// <summary>
  15. /// Summary description for WebForm1.
  16. /// </summary>
  17. public class WebForm1 : System.Web.UI.Page
  18. {
  19.       protected System.Web.UI.WebControls.TextBox nameBox;
  20.       protected System.Web.UI.WebControls.RequiredFieldValidator validateName;
  21.       protected System.Web.UI.WebControls.Calendar calendar;
  22.       protected System.Web.UI.WebControls.TextBox eventBox;
  23.       protected System.Web.UI.WebControls.RequiredFieldValidator validateEvent;
  24.       protected System.Web.UI.WebControls.DropDownList roomList;
  25.       protected System.Web.UI.WebControls.RequiredFieldValidator validateRoom;
  26.       protected System.Web.UI.WebControls.ListBox attendeeList;
  27.       protected System.Web.UI.WebControls.RequiredFieldValidator validateAttendees;
  28.       protected System.Web.UI.WebControls.Button submitButton;
  29.       protected System.Web.UI.WebControls.ValidationSummary validationSummary;
  30.       protected System.Web.UI.WebControls.DataList eventDetails2;
  31.       protected System.Web.UI.WebControls.Label edName;
  32.       protected System.Web.UI.WebControls.Label edDate;
  33.       protected System.Web.UI.WebControls.Label edRoom;
  34.       protected System.Web.UI.WebControls.Label edAttendees;
  35.       protected System.Web.UI.WebControls.Label resultLabel;
  36.       protected System.Web.UI.WebControls.DataGrid eventDetails1;
  37.       protected System.Data.DataSet ds;
  38.       protected System.Data.DataTable eventTable;
  39. private void Page_Load(object sender, System.EventArgs e)
  40. {
  41.          ds = (DataSet)Application["ds"];
  42.          attendeeList.DataSource = ds.Tables["Attendees"];
  43.          roomList.DataSource = ds.Tables["Rooms"];
  44.          eventTable = ds.Tables["Events"];
  45.          eventDetails1.DataSource = eventTable;
  46.          eventDetails2.DataSource = eventTable;
  47.          if (!this.IsPostBack)
  48.          {
  49.             System.DateTime trialDate = System.DateTime.Now;
  50.             calendar.SelectedDate = getFreeDate(trialDate);
  51.             this.DataBind();
  52.          }
  53.          else
  54.          {
  55.             eventDetails1.DataBind();
  56.             eventDetails2.DataBind();
  57.          }
  58.       }
  59. #region Web Form Designer generated code
  60. override protected void OnInit(EventArgs e)
  61. {
  62. //
  63. // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  64. //
  65. InitializeComponent();
  66. base.OnInit(e);
  67. }
  68. /// <summary>
  69. /// Required method for Designer support - do not modify
  70. /// the contents of this method with the code editor.
  71. /// </summary>
  72. private void InitializeComponent()
  73. {    
  74.          this.calendar.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.calendar_DayRender);
  75.          this.calendar.SelectionChanged += new System.EventHandler(this.calendar_SelectionChanged);
  76.          this.submitButton.Click += new System.EventHandler(this.submitButton_Click);
  77.          this.eventDetails2.SelectedIndexChanged += new System.EventHandler(this.eventDetails2_SelectedIndexChanged);
  78.          this.Load += new System.EventHandler(this.Page_Load);
  79.       }
  80. #endregion
  81.       private void submitButton_Click(object sender, System.EventArgs e)
  82.       {
  83.          if (this.IsValid)
  84.          {
  85.             String attendees = "";
  86.             foreach (ListItem attendee in attendeeList.Items)
  87.             {
  88.                if (attendee.Selected)
  89.                {
  90.                   attendees += attendee.Text + " (" + attendee.Value + "), ";
  91.                }
  92.             }
  93.             attendees += " and " + nameBox.Text;
  94.             String dateString =
  95.                calendar.SelectedDate.Date.Date.ToShortDateString();
  96.             Service1 dataService = new Service1();
  97.             int queryResult = dataService.AddEvent(eventBox.Text,
  98.                roomList.SelectedItem.Value,
  99.                attendees,
  100.                dateString);
  101.             if (queryResult == 1)
  102.             {
  103.                resultLabel.Text = "Event Added.";
  104.                ds = dataService.GetData();
  105.                Application.Lock();
  106.                Application["ds"] = ds;
  107.                Application.UnLock();
  108.                eventTable = ds.Tables["Events"];
  109.                calendar.SelectedDate =
  110.                   getFreeDate(calendar.SelectedDate.AddDays(1));
  111.                eventDetails1.DataSource = eventTable;
  112.                eventDetails1.DataBind();
  113.                eventDetails2.DataSource = eventTable;
  114.                eventDetails2.DataBind();
  115.             }
  116.             else
  117.             {
  118.                resultLabel.Text = "Event not added due to DB access problem.";
  119.             }
  120.          }
  121.       }
  122.       private System.DateTime getFreeDate(System.DateTime trialDate)
  123.       {
  124.          if (eventTable.Rows.Count > 0)
  125.          {
  126.             System.DateTime testDate;
  127.             bool trialDateOK = false;
  128.             while (!trialDateOK)
  129.             {
  130.                trialDateOK = true;
  131.                foreach (System.Data.DataRow testRow in eventTable.Rows)
  132.                {
  133.                   testDate = (System.DateTime)testRow["EventDate"];
  134.                   if (testDate.Date == trialDate.Date)
  135.                   {
  136.                      trialDateOK = false;
  137.                      trialDate = trialDate.AddDays(1);
  138.                   }
  139.                }
  140.             }
  141.          }
  142.          return trialDate;
  143.       }
  144.       private void calendar_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
  145.       {
  146.          if (eventTable.Rows.Count > 0)
  147.          {
  148.             System.DateTime testDate;
  149.             foreach (System.Data.DataRow testRow in eventTable.Rows)
  150.             {
  151.                testDate = (System.DateTime)testRow["EventDate"];
  152.                if (testDate.Date == e.Day.Date)
  153.                {
  154.                   e.Cell.BackColor = Color.Red;
  155.                }
  156.             } 
  157.          }
  158.       }
  159.       private void calendar_SelectionChanged(object sender, System.EventArgs e)
  160.       {
  161.          System.DateTime trialDate = calendar.SelectedDate;
  162.          calendar.SelectedDate = getFreeDate(trialDate);
  163.       }
  164.       protected void eventDetails2_SelectedIndexChanged(object sender, System.EventArgs e)
  165.       {
  166.          eventDetails2.DataBind();
  167.          DataRow selectedEventRow =
  168.             eventTable.Rows[eventDetails2.SelectedIndex];
  169.          edName.Text = (string)selectedEventRow["Name"];
  170.          edDate.Text = "<b>Date:</b> " +
  171.             ((DateTime)selectedEventRow["EventDate"]).ToLongDateString();
  172.          edAttendees.Text = "<b>Attendees:</b> " +
  173.             (string)selectedEventRow["AttendeeList"];
  174.          DataRow selectedEventRoomRow =
  175.             ds.Tables["Rooms"].Rows[(int)selectedEventRow["Room"] - 1];
  176.          edRoom.Text = "<b>Room:</b> " + selectedEventRoomRow["Room"];
  177.       }
  178. }
  179. }