⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editpoll.aspx.cs

📁 一个ASP.NET下的中文内容管理和社区系统
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
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;

namespace ASPNET.StarterKit.Communities.Admin.EditVoting
{
	/// <summary>
	/// Summary description for EditPoll.
	/// </summary>
	public class EditPoll : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.CheckBox IsActive;
		protected System.Web.UI.WebControls.TextBox PollQuestion;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.CheckBox IsGlobal;
		protected System.Web.UI.WebControls.TextBox StartDate;
		protected System.Web.UI.WebControls.Calendar StartDateCalendar;
		protected System.Web.UI.WebControls.LinkButton StartDateCalendarLink;
		protected System.Web.UI.WebControls.TextBox EndDate;
		protected System.Web.UI.WebControls.Calendar EndDateCalendar;
		protected System.Web.UI.WebControls.LinkButton EndDateCalendarLink;
		protected System.Web.UI.WebControls.CheckBox DisplayResultsToPublic;
		protected System.Web.UI.WebControls.Label PollChoiceDuplicate;
		protected System.Web.UI.WebControls.DataGrid PollChoices;
		protected System.Web.UI.WebControls.TextBox AddChoice;
		protected System.Web.UI.WebControls.Button Button1;
		protected ASPNET.StarterKit.Communities.Admin.ListPicker Roles;
		protected ASPNET.StarterKit.Communities.Admin.ListPicker Sections;
		protected System.Web.UI.WebControls.Panel PollDetailsPanel;
		protected System.Web.UI.WebControls.Button Button2;
		protected System.Web.UI.WebControls.Button Button3;
	

		#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.StartDateCalendar.SelectionChanged += new System.EventHandler(this.Calendar_SelectionChanged);
			this.StartDateCalendarLink.Click += new System.EventHandler(this.CalendarToggle);
			this.EndDateCalendar.SelectionChanged += new System.EventHandler(this.Calendar_SelectionChanged);
			this.EndDateCalendarLink.Click += new System.EventHandler(this.CalendarToggle);
			this.PollChoices.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.PollChoices_ItemCommand);
			this.Button1.Click += new System.EventHandler(this.AddChoice_Click);
			this.Button2.Click += new System.EventHandler(this.Save_Click);
			this.Button3.Click += new System.EventHandler(this.Cancel_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		int Poll_ID;
	    
		//*******************************************************
		//
		// When the page first loads, hide certain form
		// fields from the user.
		//
		//*******************************************************
	    
		void Page_Load(Object s, EventArgs e) {
			if (HttpContext.Current.Request.QueryString["Poll_ID"]!=null)
				Poll_ID=Int32.Parse(HttpContext.Current.Request.QueryString["Poll_ID"]);
	    
			if (Poll_ID>0 && !IsPostBack)
				BindData();
	    
			if (Poll_ID==0)
				PollDetailsPanel.Visible=false;
	    
			if (!IsPostBack) {
				StartDateCalendar.Visible=false;
				EndDateCalendar.Visible=false;
				UpdateCalendarStatus();
			}
		}
	    
	    
		//*******************************************************
		//
		// Add a new choice to the voting poll.
		//
		//*******************************************************
	    
		void AddChoice_Click(object sender, EventArgs e) {
			PollChoiceDuplicate.Visible=false;
			if (AddChoice.Text.Trim().Length>0) {
				VotingUtility.CreatePollChoice(Poll_ID,AddChoice.Text.Trim());
				AddChoice.Text=String.Empty;
				BindPollChoices();
			}
		}
	    
	    
		//*******************************************************
		//
		// Edit a voting choice by deleting the choice, moving
		// the choice up, or moving the choice down.
		//
		//*******************************************************
	    
		void PollChoices_ItemCommand(object sender, DataGridCommandEventArgs e) {
			int pollChoice_ID = (int)PollChoices.DataKeys[e.Item.ItemIndex];
			switch (e.CommandName) {
				case "Delete":
					VotingUtility.DeletePollChoice(pollChoice_ID);
					break;
				case "Up":
					VotingUtility.UpdatePollChoiceSortOrder(pollChoice_ID,PollChoiceDirection.Up);
					break;
				case "Down":
					VotingUtility.UpdatePollChoiceSortOrder(pollChoice_ID,PollChoiceDirection.Down);
					break;
			}
			BindPollChoices();
		}
	    
	    
		//*******************************************************
		//
		// Save the voting poll to the database.
		//
		//*******************************************************
	    
		void Save_Click(object sender, EventArgs e) {
			if (Page.IsValid) {
				DateTime startDate;
				DateTime endDate;
	    
				if (StartDate.Text.Trim().Length==0)
					startDate=DateTime.MinValue;
				else
					startDate=DateTime.Parse(StartDate.Text.Trim()).ToUniversalTime();
	    
				if (EndDate.Text.Trim().Length==0)
					endDate=DateTime.MaxValue;
				else
					endDate=DateTime.Parse(EndDate.Text.Trim()).ToUniversalTime();
  
	    
				if (Poll_ID==0) {
					// Insert
					Poll_ID = VotingUtility.CreatePoll(CommunityGlobals.CommunityID,PollQuestion.Text.Trim(),startDate,endDate,IsGlobal.Checked,DisplayResultsToPublic.Checked,IsActive.Checked);
					Response.Redirect("EditPoll.aspx?Poll_ID=" + Poll_ID);
				} else {
					// Update
					VotingUtility.UpdatePoll(Poll_ID,PollQuestion.Text.Trim(),startDate,endDate,IsGlobal.Checked,DisplayResultsToPublic.Checked,IsActive.Checked);
					// Save Roles
					VotingUtility.UpdatePollRoles(Poll_ID,Roles.SelectedItems);

					// Save Sections
					VotingUtility.UpdatePollSections(Poll_ID,Sections.SelectedItems);
					// Direct home
					Response.Redirect("Default.aspx");
				}
	    
			}
		}
	    
	    
		//*******************************************************
		//
		// User selected a new date in the Calendar control.
		//
		//*******************************************************
	    
		void Calendar_SelectionChanged(object sender, EventArgs e) {
			Calendar c = (Calendar)sender;
			string date=c.SelectedDate.ToString("d");
			if (c.ID=="StartDateCalendar") {
				StartDate.Text=date;
			} else if (c.ID=="EndDateCalendar") {
				EndDate.Text=date;
			}
		}
	    
	    
		//*******************************************************
		//
		// Display or hide Calendar controls.
		//
		//*******************************************************
	    
		void UpdateCalendarStatus() {
			if (StartDateCalendar.Visible) {
				StartDateCalendarLink.Text="Hide Calendar";
				StartDate.Enabled=false;
			} else {
				StartDateCalendarLink.Text="Show Calendar";
				StartDate.Enabled=true;
			}
	    
			if (EndDateCalendar.Visible) {
				EndDateCalendarLink.Text="Hide Calendar";
				EndDate.Enabled=false;
			} else {
				EndDateCalendarLink.Text="Show Calendar";
				EndDate.Enabled=true;
			}
		}
	    
	    
		//*******************************************************
		//
		// When user clicks a Calendar link, display
		// selected Calendar.
		//
		//*******************************************************
	    
		void CalendarToggle(object sender, EventArgs e) {
			Control c = sender as Control;
			if (c!=null) {
				switch(c.ID) {
					case "StartDateCalendarLink":
						StartDateCalendar.Visible=StartDateCalendar.Visible?false:true;
						UpdateStartCalendar();
						break;
					case "EndDateCalendarLink":
						EndDateCalendar.Visible=EndDateCalendar.Visible?false:true;
						UpdateEndCalendar();
						break;
				}
				UpdateCalendarStatus();
			}
		}
	    
	    
		//*******************************************************
		//
		// Update date displayed in start date
		// Calendar control.
		//
		//*******************************************************
	    
		void UpdateStartCalendar() {
			try {
				DateTime dt = DateTime.Parse(StartDate.Text.Trim());
				StartDateCalendar.SelectedDate=dt;
				StartDateCalendar.VisibleDate=dt;
			} catch {
				// Suppress
			}
		}
	    
	    
		//*******************************************************
		//
		// Update date displayed in end date Calendar
		// control.
		//
		//*******************************************************
	    
		void UpdateEndCalendar() {
			try {
				DateTime dt = DateTime.Parse(EndDate.Text.Trim());
				EndDateCalendar.SelectedDate=dt;
				EndDateCalendar.VisibleDate=dt;
			} catch  {
				// Suppress
			}
		}
	    
	    
		//*******************************************************
		//
		// Cancel adding/editing the voting poll.
		//
		//*******************************************************
	    
		void Cancel_Click(object sender, EventArgs e) {
			Response.Redirect("default.aspx");
		}
	    
	    
		//*******************************************************
		//
		// Bind the voting poll data to the form.
		//
		//*******************************************************
	    
		void BindData() {
			// Bind Poll data
			DataRow pollRow=VotingUtility.GetPoll(Poll_ID);
			PollQuestion.Text=(string)pollRow["PollQuestion"];
			IsActive.Checked=(bool)pollRow["IsActive"];
			IsGlobal.Checked=(bool)pollRow["IsGlobal"];
			DisplayResultsToPublic.Checked=(bool)pollRow["DisplayResultsToPublic"];
	    
			// Get Roles
			Roles.SelectedItems = VotingUtility.GetPollRoles(Poll_ID);
			Roles.DataSource = UserUtility.GetAllRoles();
			Roles.DataBind();
	    
	    
			Sections.SelectedItems = VotingUtility.GetPollSections(Poll_ID);
			Sections.DataSource = SectionUtility.GetAllEnabledSections().GetOrderedSections();
			Sections.DataField = "Name";
			Sections.DataBind();
	    
			// StartDate
			if (!Convert.IsDBNull(pollRow["StartDate"])) {
				StartDateCalendar.Visible=true;
				StartDate.Text=((DateTime)pollRow["StartDate"]).ToLocalTime().ToString("d");
				UpdateStartCalendar();
			}
			// EndDate
			if (!Convert.IsDBNull(pollRow["EndDate"])) {
				EndDateCalendar.Visible=true;
				EndDate.Text=((DateTime)pollRow["EndDate"]).ToLocalTime().ToString("d");
				UpdateEndCalendar();
			}
	    
			UpdateCalendarStatus();
	    
			// Bind Poll Choices
			BindPollChoices();
		}
	    
	    
		//*******************************************************
		//
		// Bind the voting poll choices to the DataGrid.
		//
		//*******************************************************
	    
		void BindPollChoices() {
			PollChoices.DataSource=VotingUtility.GetPollChoices(Poll_ID);
			PollChoices.DataBind();
		}



	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -