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

📄 showvote.aspx.cs

📁 聊天室模块设计
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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 book08
{
	/// <summary>
	/// ShowVote 的摘要说明。
	/// </summary>
	public class ShowVote : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Panel panelMulti;
		protected System.Web.UI.WebControls.RadioButtonList rblOptions;
		protected System.Web.UI.WebControls.CheckBoxList cblOptions;
		protected System.Web.UI.WebControls.Label lblQuestion;
		protected System.Web.UI.WebControls.LinkButton lbVote;
		protected System.Web.UI.WebControls.LinkButton lbView;
		protected System.Web.UI.WebControls.Label lblStartdate;
		protected System.Web.UI.WebControls.Label lblEnddate;
		protected System.Web.UI.WebControls.Label lblTotalCount;
		protected System.Web.UI.WebControls.Panel panelMain;
		protected System.Web.UI.WebControls.Panel panelNoVote;
		protected System.Web.UI.WebControls.Panel panelInfo;
		protected System.Web.UI.WebControls.DataGrid dgOtherVotes;
		protected System.Web.UI.WebControls.Panel panelSingle;

		#region Properties

		/// <summary>
		/// 投票id
		/// </summary>
		public int VoteID
		{
			get
			{
				if (Request.QueryString["id"] == null)
				{
					return GetActiveVoteID();
				}
				else
				{
					return Convert.ToInt32(Request.QueryString["id"]);
				}
			}
		}

		/// <summary>
		/// 是否多选
		/// </summary>
		public bool IsMulti
		{
			get
			{
				string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
				SqlConnection conn = new SqlConnection(strConn);
				SqlCommand cmd = conn.CreateCommand();
				cmd.CommandText = "SELECT type FROM Vote WHERE id = " + VoteID.ToString();

				int iRet = 0;
				try
				{
					conn.Open();
					iRet = Convert.ToInt32(cmd.ExecuteScalar());

				}
				catch (SqlException ex)
				{
					Response.Write(ex.Message);
				}
				finally
				{
					conn.Close();
				}
				return (iRet == 1);
			}
		}


		#endregion

		/// <summary>
		/// 获取当前投票id
		/// </summary>
		/// <returns>id</returns>
		private int GetActiveVoteID()
		{
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "GetActiveVoteID";
			

			int iRet = -1;
			try
			{
				conn.Open();
				iRet = (int)cmd.ExecuteScalar();
			}
			catch (SqlException ex)
			{
				Response.Write(ex.Message);
			}
			finally
			{
				conn.Close();
			}
			return iRet;
		}

		
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				BindOtherVotes();
				if (VoteID < 0)
				{
					panelMain.Visible = false;
					panelNoVote.Visible = true;
					panelInfo.Visible = false;
					return;
				}
				else
				{
					panelMain.Visible = true;
					panelNoVote.Visible = false;
					panelInfo.Visible = true;
				}
				if (IsMulti)
				{
					panelMulti.Visible = true;
					panelSingle.Visible = false;
					BindMulti();
				}
				else
				{
					panelMulti.Visible = false;
					panelSingle.Visible = true;
					BindSingle();
				}

				GetVoteInfo();
			}
		}

		/// <summary>
		/// 数据绑定多选列表
		/// </summary>
		private void BindMulti()
		{
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "GetOptions";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@vote_id", VoteID);
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			DataSet ds = new DataSet();

			try
			{
				conn.Open();
				da.Fill(ds);
				cblOptions.DataSource = ds.Tables[0].DefaultView;
				cblOptions.DataTextField = "content";
				cblOptions.DataValueField = "id";
				cblOptions.DataBind();
			}
			catch (SqlException ex)
			{
				Response.Write(ex.Message);
			}
			finally
			{
				conn.Close();
			}

		}

		/// <summary>
		/// 数据绑定单选列表
		/// </summary>
		private void BindSingle()
		{
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "GetOptions";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@vote_id", VoteID);
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			DataSet ds = new DataSet();

			try
			{
				conn.Open();
				da.Fill(ds);
				rblOptions.DataSource = ds.Tables[0].DefaultView;
				rblOptions.DataTextField = "content";
				rblOptions.DataValueField = "id";
				rblOptions.DataBind();
			}
			catch (SqlException ex)
			{
				Response.Write(ex.Message);
			}
			finally
			{
				conn.Close();
			}

		}

		/// <summary>
		/// 加载投票信息
		/// </summary>
		private void GetVoteInfo()
		{
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "GetVote";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@id", VoteID);

			try
			{
				conn.Open();
				SqlDataReader dr = cmd.ExecuteReader();
				if (dr.Read())
				{
					lblQuestion.Text = dr.GetString(0);
					lblStartdate.Text = dr.GetDateTime(1).ToShortDateString();
					lblEnddate.Text = dr.GetDateTime(2).ToShortDateString();
					lblTotalCount.Text = dr.GetInt32(5).ToString();
					string strScript = string.Format(
                        "window.open('ShowResult.aspx?id={0}',null,'menubar=no,toolbar=no,width=500,height=300,fullscreen=2');", VoteID);
					lbView.Attributes.Add("onclick", strScript);
				}

			}
			catch (SqlException ex)
			{
				Response.Write(ex.Message);
			}
			finally
			{
				conn.Close();
			}
		}

		/// <summary>
		/// 数据绑定其他投票
		/// </summary>
		private void BindOtherVotes()
		{
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "GetOtherVotes";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@id", VoteID);
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			DataSet ds = new DataSet();

			try
			{
				conn.Open();
				da.Fill(ds);
				dgOtherVotes.DataSource = ds.Tables[0].DefaultView;
				dgOtherVotes.DataBind();
			}
			catch (SqlException ex)
			{
				Response.Write(ex.Message);
			}
			finally
			{
				conn.Close();
			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.lbVote.Click += new System.EventHandler(this.lbVote_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void lbVote_Click(object sender, System.EventArgs e)
		{
			if (!CheckInput())
				Response.Write("<script>alert('请至少选择一个选项!');</script>");
			else
			{
				if (Request.Cookies[VoteID.ToString()] != null)
				{
					Response.Write("<script>alert('您已经投过票了,谢谢!');</script>");
				}
				else
				{
						//存cookie
						HttpCookie MyCookie = new HttpCookie(VoteID.ToString());
						MyCookie.Value = "1";
						Response.Cookies.Add(MyCookie);
						
						//投票
						DoVote();
						Response.Write("<script>alert('投票成功!');</script>");
				}
				GetVoteInfo();
			}

		}

		/// <summary>
		/// 投票
		/// </summary>
		private void DoVote()
		{
			if (IsMulti) //多选
			{
				foreach (ListItem item in cblOptions.Items)
				{
					if (item.Selected)
					{
						string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
						SqlConnection conn = new SqlConnection(strConn);
						SqlCommand cmd = conn.CreateCommand();
						cmd.CommandText = "VoteOption";
						cmd.CommandType = CommandType.StoredProcedure;
						cmd.Parameters.Add("@id", Convert.ToInt32(item.Value));

						try
						{
							conn.Open();
							cmd.ExecuteNonQuery();
						}
						catch (SqlException ex)
						{
							Response.Write(ex.Message);
						}
						finally
						{
							conn.Close();
						}
					}
				}
			}
			else
			{
				string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
				SqlConnection conn = new SqlConnection(strConn);
				SqlCommand cmd = conn.CreateCommand();
				cmd.CommandText = "VoteOption";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add("@id", Convert.ToInt32(rblOptions.SelectedValue));

				try
				{
					conn.Open();
					cmd.ExecuteNonQuery();
				}
				catch (SqlException ex)
				{
					Response.Write(ex.Message);
				}
				finally
				{
					conn.Close();
				}
			}
		}

		/// <summary>
		/// 检查选择是否正确
		/// </summary>
		/// <returns></returns>
		private bool CheckInput()
		{
			if (IsMulti)
			{
				return (cblOptions.SelectedIndex != -1);
			}
			else
			{
				return (rblOptions.SelectedIndex != -1);
			}
		}
	}
}

⌨️ 快捷键说明

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