1,数据库得设计(很简单,只是注意触发器得设置,他帮助用户修改Details表得时候主表得SUM同样被修改)

CREATE TABLE voteMaster (
 voteID 
int primary key,--编号
 voteTitle varchar(100NOT NULL ,--项目
 voteSum int default 0--总票数


insert into voteMaster values*(1,'选举工会主席',0);
insert into voteMaster values*(1,'对网站建设得意见',0);

 
CREATE TABLE voteDetails(
 voteID 
int foreign key references voteMaster(voteID),
 voteDetailsID 
int NOT NULL ,
 voteItem varchar20 
NOT NULL ,
 voteNum 
int default 0,
 
primary key(voteID,voteDetailsID)



insert into voteDetails values (1,1,'人1',0);
insert into voteDetails values (1,2,'人2',0);
insert into voteDetails values (1,3,'人3',0);
insert into voteDetails values (1,4,'人4',0);

insert into voteDetails values (2,1,'非常好',0);
insert into voteDetails values (2,2,'',0);
insert into voteDetails values (2,3,'一般',0);
insert into voteDetails values (2,4,'很差',0);

--建立触发器。使得当Detail里边的每个用户加1的时候Master中的SUM也加1
create trigger updateMaster
on voteDetails
for update
as begin
 
update voteMaster set voteSum=voteSum+1 where voteID=(select top 1 voteID from inserted)
end


2.界面得设计.不用多介绍一个标签,一个 CheckBoxList两个BUTTON

选举工会主席(label中数据由数据库提出)
数据由数据库提出
 

3.后台代码:
DB.CS   设置数据库连接

 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Web;
 5using System.Web.Security;
 6using System.Web.UI;
 7using System.Web.UI.WebControls;
 8using System.Web.UI.WebControls.WebParts;
 9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12public class DB
13{
14    public DB()
15    {}
16    public static SqlConnection creatConnection()
17    {
18        SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog =login; Integrated Security =True");
19        return conn;
20    }

21}

22


vote.aspx.cs

 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Web;
 5using System.Web.Security;
 6using System.Web.UI;
 7using System.Web.UI.WebControls;
 8using System.Web.UI.WebControls.WebParts;
 9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12public partial class _Default : System.Web.UI.Page 
13{
14    private string voteID = "1";
15    protected void Page_Load(object sender, EventArgs e)
16    {
17//判断是否是第一次登陆,如果不是就不进行数据库操作
18        if (!IsPostBack)
19        {
20
21            SqlConnection conn = DB.creatConnection();
22            conn.Open();
23            //作用:把题目查出来放到标题栏位上去
24            SqlCommand cmd = new SqlCommand("select voteTitle from voteMaster where voteID = " + this.voteID, conn);
25            //返回首行首列并将其修改为String类型
26            string title = Convert.ToString(cmd.ExecuteScalar());
27            LebalTitle.Text = title;
28            //查询对应得选举投票条目
29            SqlCommand cmdItem = new SqlCommand("select voteDetailsID,voteItem from voteDetails where voteID = " + this.voteID, conn);
30            SqlDataReader sdr = cmdItem.ExecuteReader();
31            RadioButtonItems.DataSource = sdr;
32            //显示得是值,但是他得VALUE实际上是一个ID.
33            RadioButtonItems.DataTextField = "voteItem";
34            RadioButtonItems.DataValueField = "voteDetailsID";
35            RadioButtonItems.DataBind();
36            sdr.Close();
37            conn.Close();
38        }

39       
40    }

41    protected void ButtonVote_Click(object sender, EventArgs e)
42    {
43        SqlConnection conn = DB.creatConnection();
44        conn.Open();
45        SqlCommand cmd = new SqlCommand();
46        cmd.Connection = conn;
47        cmd.CommandText = "update voteDetails set voteNum=voteNum+1 where voteID=" + voteID + " and voteDetailsID = " + RadioButtonItems.SelectedValue;
48        cmd.ExecuteNonQuery();
49        conn.Close();
50    }

51    protected void ButtonResult_Click(object sender, EventArgs e)
52    {
53        Response.Redirect("showResult.aspx?voteid="+voteID);
54    }

55}

56


result.aspx.cs
 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12public partial class showResult : System.Web.UI.Page
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16        string voteID = Request.QueryString["voteid"].ToString();
17        SqlConnection conn = DB.creatConnection();
18        conn.Open();
19        //查询对应得选举投票条目
20        SqlCommand cmdItem = new SqlCommand("select * from voteDetails where voteID = " + voteID, conn);
21        //ExecuteReader()得到返回得所有得一个结果集合 
22        SqlDataReader sdr = cmdItem.ExecuteReader();
23        while (sdr.Read())
24        {
25            Response.Write("<font size=15>"+sdr.GetString(2)+"-"+sdr.GetInt32(3).ToString()+"</font><br>");
26        }

27        sdr.Close();
28        conn.Close();
29  
30    }

31}

32

 4.另外,要在点击BUTTON的时候判断一串CHECKBOX哪些被选中,则用CHECKBOXLIST加以下代码

 1  protected void Button1_Click(object sender, EventArgs e)
 2    {
 3        for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
 4        {
 5            if(this.CheckBoxList1.Items[i].Selected) {
 6                Response.Write(this.CheckBoxList1.Items[i].Value.ToString()+"-"+CheckBoxList1.Items[i].Text.ToString());
 7            }

 8
 9        }

10    }