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

📄 managevote.aspx.cs

📁 投票系统VS2005+SQL2000 三层结构
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.SessionState;
using System.ComponentModel;
using System.Drawing;


public partial class ManageVote : System.Web.UI.Page
{

    /// <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;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    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();
        }

    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //显示添加新投票pane1
        Panel1.Visible = true;
        //取消当前的选择状态
        dgVotes.SelectedIndex = -1;
        BindGrid();
    }
    protected void lbAdd_Click(object sender, 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();
            this.Panel1.Visible = false;
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            conn.Close();
        }

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

        //if (e.CommandName == "EditOption") //单击“修改选项”按钮
        //{  
        //    //若单击另一条投票信息,则切换到单击的这条投票信息
       
        
        //    BindGrid();

        //    if (dgVotes.SelectedIndex != -1)
        //    {
        //        //求得当前选中的投票ID,注意区分显示和编辑两种状态 
           
            

        //        //显示修改选项的panel
        //       this.Panel2.Visible = true;
        ////        //隐藏增加选项的panel
        //       this.Panel1.Visible = false;
        //        //显示选中投票项目的选项列表
        //        BindOptionGrid();
        //    }
        //    else
        //    {
        //        //更新SelectedVoteID
        //      //  SelectedVoteID = -1;
        //        //隐藏修改选项的panel
        //        panelOption.Visible = false;
        //    }
        //}
       // this.dgVotes.DataKeys[e.RowIndex].Value.ToString();
      // string s=this.dgVotes.SelectedRow.Cells[0]..ToString();
        
       // Response.Write(s);

    }


    /// <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();
        }

    }


    protected void dgVotes_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int id = e.NewSelectedIndex;
        Label idd=(Label)this.dgVotes.Rows[id].Cells[0].FindControl("id");
       // Response.Write(idd.Text);
       // dgVotes.EditIndex = id;
        
        
            //若单击另一条投票信息,则切换到单击的这条投票信息
            //若单击当前选中的投票信息,则退出编辑选项的状态
            if (dgVotes.SelectedIndex != e.NewSelectedIndex)
                dgVotes.SelectedIndex = e.NewSelectedIndex;
            else
                dgVotes.SelectedIndex = -1;
            BindGrid();

            if (dgVotes.SelectedIndex != -1)
            {
               
                int vote_id = Convert.ToInt32(idd.Text);
                SelectedVoteID = vote_id;

                //显示修改选项的panel
               this.Panel2.Visible = true;
                //隐藏增加选项的panel
                this.Panel1.Visible = false;
                //显示选中投票项目的选项列表
                BindOptionGrid();
            }
      




    }

    protected void dgVotes_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int index = e.NewEditIndex;
        Label idd = (Label)this.dgVotes.Rows[index].Cells[0].FindControl("id");
        EditVoteID = Convert.ToInt32(idd.Text);
        dgVotes.EditIndex = index;
        BindGrid();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {

    }
    protected void dgVotes_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //获取编辑后的各字段值
        //DataGridItem item = dgVotes.d;
        GridViewRow item = dgVotes.Rows[e.RowIndex];
       
        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.EditIndex = -1;
            BindGrid();
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            conn.Close();
        }


    }
    protected void dgVotes_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgVotes.EditIndex = -1;
        BindGrid();
    }
    protected void dgVotes_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //获取准备删除的投票ID

        int index = e.RowIndex;
        Label hih = (Label)this.dgVotes.Rows[index].Cells[0].FindControl("id");
        if (hih == null)
            return;

        //执行存储过程,删除投票
        int vote_id = Convert.ToInt32(hih.Text);
        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.PageIndex = 0;
        BindGrid();
    }
    protected void dgOptions_RowEditing(object sender, GridViewEditEventArgs e)
    {
        EditOptionID = Convert.ToInt32(((Label)this.dgOptions.Rows[e.NewEditIndex].Cells[0].FindControl("id")).Text);
        dgOptions.EditIndex = e.NewEditIndex;
        BindOptionGrid();
    }
    protected void dgOptions_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        
        dgOptions.EditIndex = -1;
        BindOptionGrid();
    }
    protected void dgOptions_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        //更新选项内容,刷新选项列表
        TextBox tb = (TextBox)this.dgOptions.Rows[e.RowIndex].Cells[1].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.EditIndex = -1;
            BindOptionGrid();
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            conn.Close();
        }

    }
    protected void dgOptions_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        //求得将要删除的选项ID
        int index = e.RowIndex;
        //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");
        //}
        Label  hih= (Label)dgOptions.Rows[index].Cells[0].FindControl("id");//Convert.ToInt32(hih.Value);
        int option_id=Convert.ToInt32(hih.Text);
        //删除选项,刷新选项列表
        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();
        }

    }
    protected void LinkButton5_Click(object sender, 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 LinkButton2_Click1(object sender, EventArgs e)
    {
        //跳转到投票显页
        Response.Redirect("~/ShowVote.aspx");
    }
}

⌨️ 快捷键说明

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