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

📄 managevote.aspx.cs

📁 投票功能的完整实现... 投票功能的完整实现...
💻 CS
📖 第 1 页 / 共 2 页
字号:
			//获取准备删除的投票ID
			HtmlInputHidden hih = (HtmlInputHidden)(e.Item.Cells[0].FindControl("vote_id"));
			if (hih == null)
				return;

			//执行存储过程,删除投票
			int vote_id = Convert.ToInt32(hih.Value);
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "DeleteVote";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@id", vote_id);

			try
			{
				conn.Open();
				cmd.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				Response.Write(ex.Message + vote_id.ToString());
			}
			finally
			{
				conn.Close();
			}
			
			dgVotes.CurrentPageIndex = 0;
			BindGrid();		
		}

		private void dgVotes_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if (e.CommandName == "EditOption") //单击“修改选项”按钮
			{
				//若单击另一条投票信息,则切换到单击的这条投票信息
				//若单击当前选中的投票信息,则退出编辑选项的状态
				if (dgVotes.SelectedIndex != e.Item.ItemIndex)
					dgVotes.SelectedIndex = e.Item.ItemIndex;
				else
					dgVotes.SelectedIndex = -1;
				BindGrid();

				if (dgVotes.SelectedIndex != -1)
				{
					//求得当前选中的投票ID,注意区分显示和编辑两种状态 
					HtmlInputHidden hih = null;
					if (dgVotes.EditItemIndex != e.Item.ItemIndex)
					{
						hih = (HtmlInputHidden)(e.Item.Cells[0].FindControl("vote_id"));
					}
					else
					{
						hih = (HtmlInputHidden)(e.Item.Cells[0].FindControl("vote_id_Edit"));
					}
					int vote_id = Convert.ToInt32(hih.Value);
					SelectedVoteID = vote_id;

					//显示修改选项的panel
					panelOption.Visible = true;
					//隐藏增加选项的panel
					panelAddVote.Visible = false;
					//显示选中投票项目的选项列表
					BindOptionGrid();
				}
				else
				{
					//更新SelectedVoteID
					SelectedVoteID = -1;
					//隐藏修改选项的panel
					panelOption.Visible = false;
				}
			}
		}


		#endregion

		#region dgOptions

		private void dgOptions_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			//单击删除选项按钮时弹出确认提示
			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[2].Controls[0]);
				lb.Attributes.Add("onclick", "return confirm('您确定要删除该选项吗?');");;
			}
		}

		private void dgOptions_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			//求得将要删除的选项ID
			int index = e.Item.ItemIndex;
			DataGridItem item = dgOptions.Items[index];
			HtmlInputHidden hih = null;
			if (dgOptions.EditItemIndex != e.Item.ItemIndex)
			{
				hih = (HtmlInputHidden)item.Cells[0].FindControl("option_id");
			}
			else
			{
				hih = (HtmlInputHidden)item.Cells[0].FindControl("option_id_Edit");
			}
			int option_id = Convert.ToInt32(hih.Value);

			//删除选项,刷新选项列表
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "DeleteOption";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@id", option_id);

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

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

			//求编辑的选项ID
			HtmlInputHidden hih = (HtmlInputHidden)dgOptions.Items[index].Cells[0].FindControl("option_id");
			EditOptionID = Convert.ToInt32(hih.Value);

			//进入编辑状态
			dgOptions.EditItemIndex = index;
			BindOptionGrid();			
		}

		private void dgOptions_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			//退出编辑状态
			dgOptions.EditItemIndex = -1;
			BindOptionGrid();
		}

		private void dgOptions_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			//更新选项内容,刷新选项列表
			TextBox tb = (TextBox)e.Item.Cells[0].FindControl("tbContentEdit");			

			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "UpdateOption";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@id", EditOptionID);
			cmd.Parameters.Add("@content", tb.Text);

			try
			{
				conn.Open();
				cmd.ExecuteNonQuery();

				dgOptions.EditItemIndex = -1;
				BindOptionGrid();
			}
			catch (SqlException ex)
			{
				Response.Write(ex.Message);
			}
			finally
			{
				conn.Close();
			}
		}


		#endregion

		#region button clicks

		protected void lbAddVote_Click(object sender, System.EventArgs e)
		{
			//显示添加新投票panel
			panelAddVote.Visible = true;
			//隐藏编辑选项panel
			panelOption.Visible = false;
			//取消当前的选择状态
			dgVotes.SelectedIndex = -1;
			BindGrid();
		}

		protected void lbAdd_Click(object sender, System.EventArgs e)
		{
			//添加新投票项目
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "AddVote";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@question", tbQuestionAdd.Text);
			cmd.Parameters.Add("@start_date", tbStartdateAdd.Text);
			cmd.Parameters.Add("@end_date", tbEnddateAdd.Text);
			cmd.Parameters.Add("@type", cbMultiAdd.Checked);
			cmd.Parameters.Add("@active", cbActiveAdd.Checked);

			try
			{
				conn.Open();
				cmd.ExecuteNonQuery();
				BindGrid();
				panelAddVote.Visible = false;
			}
			catch (SqlException ex)
			{
				Response.Write(ex.Message);
			}
			finally
			{
				conn.Close();
			}
		}

		protected void lbCancel_Click(object sender, System.EventArgs e)
		{
			//隐藏添加新投票的panel
			panelAddVote.Visible = false;
		}

		protected void lbAddOption_Click(object sender, System.EventArgs e)
		{
			//执行存储过程,添加投票的选项,并刷新数据显示
			string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn = new SqlConnection(strConn);
			SqlCommand cmd = conn.CreateCommand();
			cmd.CommandText = "AddOption";
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("@vote_id", SelectedVoteID);
			cmd.Parameters.Add("@content", tbNewOption.Text);

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

		protected void lbReturn_Click(object sender, System.EventArgs e)
		{
			//跳转到投票显示页
			Response.Redirect("~/ShowVote.aspx");
		}


		#endregion

	}
}

⌨️ 快捷键说明

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