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 WebMain_vote_vote : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            databind();
        }
    }
    protected void databind()
    { 
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand smd = new SqlCommand("select top 1 * from vote where isfp='y'",conn);
        SqlDataReader sdr = smd.ExecuteReader();
        sdr.Read();
        if (sdr.HasRows)
        {
            Session["voteid"] = sdr["id"].ToString();
            this.Label1.Text = sdr["title"].ToString();
            this.t1.Text = sdr["q1"].ToString();
            this.t2.Text = sdr["q2"].ToString();
            this.t3.Text = sdr["q3"].ToString();
            this.t4.Text = sdr["q4"].ToString();
            this.t5.Text = sdr["q5"].ToString();
            if (sdr["qtv"] != "y")
            {
                this.Button2.Visible = false;
            }
        }
        else
        {
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
        }
        sdr.Close();
        conn.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["vote"] != null)
        {

            if (Session["vote"] == "1")
            {
                string iswhere = "";
                Session["vote"] = 2;
                if (t1.Checked)
                {
                    Doadmin.Execsql("update vote set d1=d1+1 where id=" + Session["voteid"]);
                    Doadmin.Execsql("update vote set votoall=votoall+1 where id=" + Session["voteid"]);
                }
                if (t2.Checked)
                {
                    Doadmin.Execsql("update vote set d2=d2+1 where id=" + Session["voteid"]);
                    Doadmin.Execsql("update vote set votoall=votoall+1 where id=" + Session["voteid"]);
                }
                if (t3.Checked)
                {
                    Doadmin.Execsql("update vote set d3=d3+1 where id=" + Session["voteid"]);
                    Doadmin.Execsql("update vote set votoall=votoall+1 where id=" + Session["voteid"]);
                }
                if (t4.Checked)
                {
                    Doadmin.Execsql("update vote set d4=d4+1 where id=" + Session["voteid"]);
                    Doadmin.Execsql("update vote set votoall=votoall+1 where id=" + Session["voteid"]);
                }
                if (t5.Checked)
                {
                    Doadmin.Execsql("update vote set d5=d5+1 where id=" + Session["voteid"]);
                    Doadmin.Execsql("update vote set votoall=votoall+1 where id=" + Session["voteid"]);
                }
                Response.Write("<script>alert('投票成功!感谢您的投票!');window.location.href='Default.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('您刚投过票了,请休息会吧.');window.location.href='Default.aspx'</script>");
            }
        }
        else
        {
            Response.Redirect("~/");
        }
    }
}
