Web控件学习之投票实例

来源:互联网 发布:手机扩音器软件下载 编辑:程序博客网 时间:2024/05/16 07:46

实现目标

  • 单击投票,给选定目标加一票。
  • 单击结果,显示项目总票数。
  • 如图

实现代码

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data ;using System.Data.SqlClient ;namespace Vote{    public partial class Vote : System.Web.UI.Page    {        //定义变量,当前投票的项目编号        private string voteID = "2";        protected void Page_Load(object sender, EventArgs e)        {            if (!this.IsPostBack)            {                Label1.Text = "";                //创建一个连接对象                //SqlConnection con = new SqlConnection("server=192.168.24.153 ;dataBase=vote;uid=sa;pwd=123456;");                SqlConnection con = DB.createConnection();                con.Open();                //查询选举的标题                SqlCommand cmd = new SqlCommand("select voteTitle from voteMaster where voteID=" + this.voteID, con);                string title = Convert.ToString(cmd.ExecuteScalar()); //执行查询,并返回查询结果中的第一行的第一列                this.lblTitle.Text = title;  //显示选举的标题                //查询对应的投票                SqlCommand cmdItem = new SqlCommand("select voteDetailsID,voteItem from voteDetails where voteID=" + voteID, con);                SqlDataReader sdr = cmdItem.ExecuteReader();                //绑定到rbtnItem上                //获取数据源                this.rBtnItem.DataSource = sdr;                //获取在rbtnItem中显示的数据字段                this.rBtnItem.DataTextField = "voteItem";                //获取为rbtnItem的值选择数据字段                this.rBtnItem.DataValueField = "voteDetailsID";                //绑定                this.rBtnItem.DataBind();                sdr.Close();                //关闭连接                con.Close();            }                  }        protected void btnVote_Click(object sender, EventArgs e)        {            SqlConnection con = DB.createConnection();            con.Open();            SqlCommand cmd = new SqlCommand();            cmd.Connection = con;            cmd .CommandText ="update voteDetails set voteNum=voteNum+1 where voteID="+voteID + "  and voteDetailsID="+this.rBtnItem.SelectedValue.ToString ();            //执行SQL语句            cmd.ExecuteNonQuery();            con.Close();        }        protected void btnShowResult_Click(object sender, EventArgs e)        {             //创建一个连接对象                //SqlConnection con = new SqlConnection("server=192.168.24.153 ;dataBase=vote;uid=sa;pwd=123456;");                SqlConnection con = DB.createConnection();                con.Open();                //查询选举的标题                SqlCommand cmd = new SqlCommand("select voteSum from voteMaster where voteID=" + this.voteID, con);                string sum = Convert.ToString(cmd.ExecuteScalar()); //执行查询,并返回查询结果中的第一行的第一列                this.Label1.Text = "总票数为:" +sum;        }    }}


DB类

    public static SqlConnection createConnection()    {        SqlConnection con = new SqlConnection("server=. ;dataBase=vote;uid=sa;pwd=123456;");        return con;    }


 

界面设计

 

原创粉丝点击