using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;

public partial class Ex6_10 : System.Web.UI.Page
{
    //定义静态全局变量strcon和voteid
    static string strcon = System.Configuration.ConfigurationManager.AppSettings["strcon"].ToString();
    static int voteid;
    OleDbConnection ocon = new OleDbConnection(strcon);
    protected void Page_Load(object sender, EventArgs e)
    {
        //判断页面是否为第一次加载
        if (!IsPostBack)
        {
            OleDbDataAdapter oda = new OleDbDataAdapter("select top 1 * from votename", ocon);
            DataSet ds = new DataSet();
            oda.Fill(ds);
            //获取满足条件的第1行记录
            DataRow dr = ds.Tables[0].Rows[0];
            lbltitle.Text = dr[1].ToString();
            voteid = Convert.ToInt32(dr[0]);
            //绑定数据表到投票选项
            OleDbDataAdapter oda2 = new OleDbDataAdapter("select * from voteitems where vid=" + voteid, ocon);
            DataSet ds2 = new DataSet();
            oda2.Fill(ds2);
            rdblvote.DataSource = ds2;
            rdblvote.DataTextField = "vitmtitle";
            rdblvote.DataValueField = "vitmid";
            rdblvote.DataBind();
        }
    }
    protected void btnvote_Click(object sender, EventArgs e)
    {
        //判断用户是否选择了投票选项
        if (rdblvote.SelectedItem !=null )
        {
            //修改投票选项的得票数
            string sqlstr = "update voteitems set vitmnum=vitmnum+1 where vitmid=" +Convert.ToInt32(rdblvote.SelectedValue);
            ocon.Open();
            OleDbCommand ocmd = new OleDbCommand(sqlstr, ocon);
            ocmd.ExecuteNonQuery();
            ocon.Close();
            Response.Write("<script>alert('投票成功，感谢你的参与！');</script>");
            Response.Redirect("Ex6-10(2).aspx?vid=" + voteid);
        }
        else
        {
            Response.Write("<script>alert('你没有选择选项！');</script>");
        }
    }
}
