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

📄 showvote.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;

public partial class ShowVote : System.Web.UI.Page
{
    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);
        }
    }

    
    
    protected void Page_Load(object sender, 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>
    /// 获取当前投票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;
    }


  

    /// <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);
           // ddgOtherVotes.DataSource = ds.Tables[0].DefaultView;
          //  ddgOtherVotes.DataBind();
            this.dgOtherVotes.DataSource = ds.Tables[0].DefaultView;
           this.dgOtherVotes.DataBind();
            
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }

 



    /// <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);
        }
    }
    protected void lbView_Click(object sender, EventArgs e)
    {

    }
    protected void lbVote_Click1(object sender, 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();
        }

    }
}

⌨️ 快捷键说明

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