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

📄 managevote.aspx.cs

📁 投票功能的完整实现... 投票功能的完整实现...
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections;
using System.ComponentModel;
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;
using System.Configuration;

namespace book08
{
	/// <summary>
	/// ManageVote 的摘要说明。
	/// </summary>
	public partial class ManageVote : System.Web.UI.Page
	{
		#region protected members


		#endregion

		#region Properties

		/// <summary>
		/// 当前选中投票的id
		/// </summary>
		public int SelectedVoteID
		{
			get
			{
				if (ViewState["SelectedVoteID"] == null)
					return -1;
				return Convert.ToInt32(ViewState["SelectedVoteID"]);
			}
			set
			{
				ViewState["SelectedVoteID"] = value;
			}
		}

		/// <summary>
		/// 当前编辑投票的id,利用ViewState存取当前编辑的投票ID
		/// </summary>
		public int EditVoteID
		{
			get
			{
				if (ViewState["EditVoteID"] == null)
					return -1;
				return Convert.ToInt32(ViewState["EditVoteID"]);
			}
			set
			{
				ViewState["EditVoteID"] = value;
			}
		}

		/// <summary>
		/// 当前编辑的选项id
		/// </summary>
		public int EditOptionID
		{
			get
			{
				if (ViewState["EditOptionID"] == null)
					return -1;
				return Convert.ToInt32(ViewState["EditOptionID"]);
			}
			set
			{
				ViewState["EditOptionID"] = value;
			}
		}


		#endregion

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				//显示投票列表
				BindGrid();
				//显示当前选中投票的选项内容,若没有选中则不显示
				panelOption.Visible = (SelectedVoteID != -1);
				//隐藏增加新选项的panel
				panelAddVote.Visible = false;
			}

            //为添加选项按钮增加客户端属性onclick,防止提交空选项
			string newOptionID = tbNewOption.UniqueID.ToString();
			string strScript = string.Format(
                "return (document.all('{0}').value != '');", newOptionID);
			lbAddOption.Attributes.Add("onclick", strScript);

		}

		/// <summary>
		/// 数据绑定
		/// </summary>
		private void BindGrid()
		{
			//获取所有投票的信息并显示
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
            SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "GetAllVotes";
			cmd.CommandType = CommandType.StoredProcedure;
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			DataSet ds = new DataSet();

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

		}

		/// <summary>
		/// 数据绑定选项列表
		/// </summary>
		private void BindOptionGrid()
		{
			//获取所有选项的信息并显示
			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", SelectedVoteID);

			SqlDataAdapter da = new SqlDataAdapter(cmd);
			DataSet ds = new DataSet();

			try
			{
				conn.Open();
				da.Fill(ds);
				dgOptions.DataSource = ds.Tables[0].DefaultView;
				dgOptions.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.dgVotes.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgVotes_ItemCreated);
			this.dgVotes.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgVotes_ItemCommand);
			this.dgVotes.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgVotes_CancelCommand);
			this.dgVotes.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgVotes_EditCommand);
			this.dgVotes.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgVotes_UpdateCommand);
			this.dgVotes.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgVotes_DeleteCommand);
			this.dgOptions.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgOptions_ItemCreated);
			this.dgOptions.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgOptions_CancelCommand);
			this.dgOptions.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgOptions_EditCommand);
			this.dgOptions.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgOptions_UpdateCommand);
			this.dgOptions.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgOptions_DeleteCommand);

		}
		#endregion

		#region dgVotes

		private void dgVotes_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			//编辑项在dgVotes中的序号
			int index = e.Item.ItemIndex;

			//获取当前编辑的投票ID
			HtmlInputHidden hih = (HtmlInputHidden)dgVotes.Items[index].Cells[0].FindControl("vote_id");
			EditVoteID = Convert.ToInt32(hih.Value);

			//将当前行转为编辑状态
			dgVotes.EditItemIndex = index;
			BindGrid();

		}

		private void dgVotes_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			//获取编辑后的各字段值
			DataGridItem item = dgVotes.Items[e.Item.ItemIndex];
			string strQuestion = ((TextBox)item.FindControl("tbQuestion")).Text;
			string strStartdate = ((TextBox)item.FindControl("tbStartdate")).Text;
			string strEnddate = ((TextBox)item.FindControl("tbEnddate")).Text;
			bool bMulti = ((CheckBox)item.FindControl("cbMultiEdit")).Checked;
			bool bActive = ((CheckBox)item.FindControl("cbActiveEdit")).Checked;

			//执行存储过程更新投票信息,同时退出编辑状态
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "UpdateVote";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@id", EditVoteID);
			cmd.Parameters.Add("@question", strQuestion);
			cmd.Parameters.Add("@start_date", strStartdate);
			cmd.Parameters.Add("@end_date", strEnddate);
			cmd.Parameters.Add("@type", bMulti);
			cmd.Parameters.Add("@active", bActive);

			try
			{
				conn.Open();
				cmd.ExecuteNonQuery();
				dgVotes.EditItemIndex = -1;
				BindGrid();
			}
			catch (SqlException ex)
			{
				Response.Write(ex.Message);
			}
			finally
			{
				conn.Close();
			}
		}

		private void dgVotes_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			//退出编辑状态,不做任何操作
			dgVotes.EditItemIndex = -1;
			BindGrid();
		}

		private void dgVotes_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			//为删除按钮增加客户端onclick属性
			if (e.Item.ItemType	== ListItemType.Item ||
				e.Item.ItemType	== ListItemType.AlternatingItem ||
				e.Item.ItemType == ListItemType.EditItem ||
				e.Item.ItemType == ListItemType.SelectedItem)
			{
				LinkButton lb = (LinkButton)(e.Item.Cells[7].Controls[0]);
				lb.Attributes.Add("onclick", "return confirm('您确定要删除该项投票吗?');");;
			}

		}

		private void dgVotes_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{

⌨️ 快捷键说明

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