前几天完成老师的作业,网上投票系统,今天终于做完了,特地把代码分享一下。
第一个页面代码:
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Collections; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Web.UI.HtmlControls; 11 using System.Data.SqlClient; 12 13 public partial class ShowVote : System.Web.UI.Page 14 { 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 Bind(); 18 } 19 public void Bind() 20 { 21 SqlConnection con = new SqlConnection(); 22 con.ConnectionString = " Data Source=.;database=VoteDB;integrated security=true; "; 23 string sql = "select * from votetitle where status=1"; 24 SqlDataAdapter sda = new SqlDataAdapter(); 25 sda.SelectCommand = new SqlCommand(sql, con); 26 DataSet ds = new DataSet(); 27 sda.Fill(ds, "TitleTable"); 28 sql = "select * from voteitem where titleid=(select titleid from votetitle where status=1)"; 29 sda.SelectCommand = new SqlCommand(sql, con); 30 sda.Fill(ds, "ItemTable"); 31 DataView dv = ds.Tables["ItemTable"].DefaultView; 32 dv.Sort = "ItemID asc"; 33 string Title = ds.Tables["TitleTable"].Rows[0]["Title"].ToString(); 34 Label1.Text = Title; 35 bool Mode = Convert.ToBoolean(ds.Tables["TitleTable"].Rows[0]["Mode"]); 36 HiddenField1.Value = Mode.ToString(); 37 if (Mode) 38 { 39 CheckBoxList obj=new CheckBoxList (); 40 obj.BackColor = System.Drawing.ColorTranslator.FromHtml("#EDEDED"); 41 obj.ID = "listVoteItem"; 42 obj.Width = 180; 43 obj.DataSource = dv; 44 obj.DataTextField = "Item"; 45 obj.DataValueField = "ItemID"; 46 obj.DataBind(); 47 Panel1.Controls.Add(obj); 48 } 49 else 50 { 51 RadioButtonList obj=new RadioButtonList (); 52 obj.BackColor = System.Drawing.ColorTranslator.FromHtml("#EDEDED"); 53 obj.ID = "listVoteItem"; 54 obj.Width = 180; 55 obj.DataSource = dv; 56 obj.DataTextField = "Item"; 57 obj.DataValueField = "ItemID"; 58 obj.DataBind(); 59 Panel1.Controls.Add(obj); 60 } 61 } 62 protected void Button1_Click(object sender, EventArgs e) 63 { 64 string pass = null; 65 if (Request.Cookies["pass"] == null) 66 { 67 pass = ""; 68 } 69 else 70 { 71 pass = Request.Cookies["pass"].Value.ToString(); 72 } 73 if (pass == "pass") 74 { 75 Response.Write("<script language='javascript'>alert('你每天只能投票一次');history.back();</script>"); 76 Response.End(); 77 } 78 else 79 { 80 SqlConnection conn = new SqlConnection(); 81 conn.ConnectionString = " Data Source=.;database=VoteDB;integrated security=true; "; 82 conn.Open(); 83 SqlCommand cmd=new SqlCommand (); 84 cmd.Connection = conn; 85 cmd.CommandType = CommandType.Text; 86 bool Mode = Convert.ToBoolean(HiddenField1.Value); 87 if (Mode) 88 { 89 CheckBoxList rb = (CheckBoxList)Panel1.FindControl("listVoteItem"); 90 if (rb.SelectedIndex == -1) 91 { 92 Response.Write("<script language='javascript'>alert('你必须至少选择一项');history.back();</script>"); 93 Response .End (); 94 } 95 else 96 { 97 foreach(ListItem item in rb.Items ) 98 { 99 if(item .Selected ) 100 { 101 cmd.CommandText ="update VoteItem set Number=Number+1 where ItemID="+item.Value.ToString (); 102 cmd.ExecuteNonQuery (); 103 } 104 } 105 Response .Cookies ["pass"].Value ="pass"; 106 Response .Cookies ["pass"].Expires =DateTime .Now.AddDays (1); 107 Response .Write ("<script language='javascript'>alert('投票成功');history.back();</script>"); 108 109 } 110 111 112 } 113 else 114 { 115 RadioButtonList rb=(RadioButtonList )Panel1 .FindControl ("listVoteItem"); 116 if(rb.SelectedIndex ==-1) 117 { 118 Response .Write ("<script language='javascript'>alert('你必须至少选择一票');history.back();</script>"); 119 Response .End (); 120 } 121 else 122 { 123 foreach (ListItem item in rb.Items ) 124 { 125 if(item.Selected ) 126 { 127 cmd.CommandText ="update VoteItem set Number=Number+1 where ItemID="+item.Value .ToString (); 128 cmd.ExecuteNonQuery (); 129 } 130 } 131 } 132 Response .Cookies ["pass"].Value ="pass"; 133 Response .Cookies ["pass"].Expires =DateTime .Now.AddDays (1); 134 Response .Write ("<script language='javascript'>alert('投票成功');history.back();</script>"); 135 } 136 conn.Close (); 137 conn.Dispose (); 138 139 140 } 141 } 142 protected void Button2_Click(object sender, EventArgs e) 143 { 144 Response.Redirect("LookVote.aspx"); 145 } 146 }
HTML代码:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowVote.aspx.cs" Inherits="ShowVote" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml" > 6 <head runat="server"> 7 <title>梁德林投票系统INDEX</title> 8 </head> 9 <body> 10 <form id="form1" runat="server"> 11 <div> 12 <table style=" 473px; height: 209px"> 13 <tr> 14 <td style=" 51px"> 15 主题:</td> 16 <td colspan="2"> 17 <asp:Label ID="Label1" runat="server" Width="182px"></asp:Label></td> 18 </tr> 19 <tr> 20 <td rowspan="3" style=" 51px"> 21 项目:</td> 22 <td colspan="2" rowspan="2" style="height: 142px"> 23 <asp:HiddenField ID="HiddenField1" runat="server" /> 24 <asp:Panel ID="Panel1" runat="server"> 25 <br /> 26 </asp:Panel> 27 </td> 28 </tr> 29 <tr> 30 </tr> 31 <tr> 32 <td colspan="2" rowspan="1" style="height: 13px"> 33 <asp:Button ID="Button1" runat="server" Text="投票" Width="78px" OnClick="Button1_Click" /> 34 35 <asp:Button ID="Button2" runat="server" Text="查看" Width="72px" OnClick="Button2_Click" /></td> 36 </tr> 37 </table> 38 39 </div> 40 </form> 41 </body> 42 </html>
第二个页面代码:
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Collections; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Web.UI.HtmlControls; 11 using System.Data.SqlClient; 12 13 public partial class LookVote : System.Web.UI.Page 14 { 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 SqlConnection con = new SqlConnection(); 18 con.ConnectionString = " Data Source=.;database=VoteDB;integrated security=true; "; 19 string sql = "select a.Title,a.SumNumber,a.Mode,a.Status,b.*,case a.SumNumber when 0 then 0 else round(cast(b.Number as float)/cast(a.SumNumber as float)*100,2) end as ItemPercent from VoteTitle a,VoteItem b where a.TitleID=b.TitleID and a.Status=1"; 20 SqlDataAdapter sda = new SqlDataAdapter(sql, con); 21 DataSet ds = new DataSet(); 22 sda.Fill(ds); 23 Repeater1.DataSource = ds.Tables[0].DefaultView; 24 Repeater1.DataBind(); 25 lblInfo.Text = "共有" + ds.Tables[0].Rows[0]["SumNumber"].ToString(); 26 lblTitle.Text = ds.Tables[0].Rows[0]["Title"].ToString(); 27 } 28 }
HTML代码:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LookVote.aspx.cs" Inherits="LookVote" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head id="Head1" runat="server"> 7 <title>无标题页</title> 8 </head> 9 <body style=" text-align:center; background-color:#f9cd34"> 10 <form id="form1" runat="server" style=" text-align:center;"> 11 <div style=" 500px; text-align:center; background-color:#FFF2BB;"> 12 <br /> 13 <table style="color:#333333;text-align:left; border:0; 90%; background-color:#b18a02; font-size:12px; font-family:宋体;" cellpadding="2" cellspacing="1"> 14 <tr style=" height:25px; background-color:#FFFFFF"> 15 <td> 16 <br /> 17 <asp:Label ID="lblInfo" runat="server"></asp:Label> 18 </td> 19 </tr> 20 <tr style=" height:25px; background-color:#f9dc34"> 21 <td>主题:<asp:Label ID="lblTitle" runat="server"></asp:Label></td> 22 </tr> 23 </table> 24 25 <asp:Repeater ID="Repeater1" runat="server"> 26 <HeaderTemplate> 27 <table style="color:#333333;text-align:left; border:0; 90%; background-color:#b18a02; font-size:12px; font-family:宋体;" cellpadding="2" cellspacing="1"> 28 <tr style=" height:25px; background-color:#FFFFFF; text-align:center;"> 29 <td style=" 50px;"> </td> 30 <td style=" 170px;">选项</td> 31 <td style=" 170px;">比例</td> 32 <td style=" 110px;">票数</td> 33 </tr> 34 </HeaderTemplate> 35 <ItemTemplate> 36 <tr style=" height:25px; background-color:#FFFFFF; text-align:left;"> 37 <td style=" text-align:center;"><%#DataBinder.Eval(Container.DataItem,"ItemID") %></td> 38 <td><%#DataBinder.Eval(Container.DataItem,"Item") %></td> 39 <td><%#DataBinder.Eval(Container.DataItem, "ItemPercent", "<img src='Images/vote.gif' height='15' width='{0}'>")%> 40 <%#DataBinder.Eval(Container.DataItem,"ItemPercent","{0}%") %></td> 41 <td style=" text-align:right;"><%#DataBinder.Eval(Container.DataItem,"Number") %></td> 42 </tr> 43 </ItemTemplate> 44 <FooterTemplate> 45 <tr style=" height:25px; background-color:#FFFFFF; text-align:center;"> 46 <td colspan="4"> 47 <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="javascript:window.print(); return false;">[打印]</asp:LinkButton> 48 <asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="javascript:window.close(); return false;">[关闭]</asp:LinkButton> 49 </td> 50 </tr> 51 </table></FooterTemplate> 52 </asp:Repeater> 53 <br /> 54 </div> 55 </form> 56 </body> 57 </html>
第一个页面ShowVote.aspx截图:
第二个页面LookVote.aspx截图:
数据库设计代码:
1 use master 2 go 3 create database VoteDB--创建数据库 VoteDB 4 go 5 use VoteDB--使用数据库 VoteDB 6 go 7 create table VoteTitle 8 ( 9 TitleID int identity primary key, 10 Title varchar(200) not null, 11 SumNumber int default 0, 12 Mode bit default 0, 13 Status bit default 0 14 ) 15 go 16 insert into VoteTitle(Title,Mode,Status) values('你对我们网站评价如何?',0,1) 17 insert into VoteTitle(Title,Mode,Status) values('请选择你喜欢的编程语言有哪些?',0,0) 18 go 19 create table VoteItem 20 ( 21 ItemID int identity primary key, 22 Item varchar(200) not null, 23 Number int default 0, 24 TitleID int references VoteTitle(TitleID) on delete cascade on update cascade 25 ) 26 go 27 insert into VoteItem(Item,TitleID) values('非常好',1) 28 insert into VoteItem(Item,TitleID) values('好',1) 29 insert into VoteItem(Item,TitleID) values('一般',1) 30 insert into VoteItem(Item,TitleID) values('差',1) 31 insert into VoteItem(Item,TitleID) values('非常差',1) 32 go 33 insert into VoteItem(Item,TitleID) values('ASP.NET Web开发语法',2) 34 insert into VoteItem(Item,TitleID) values('JSP开发语言',2) 35 insert into VoteItem(Item,TitleID) values('PHP开发语言',2) 36 insert into VoteItem(Item,TitleID) values('ASP开发语言',2) 37 insert into VoteItem(Item,TitleID) values('C#开发语言',2) 38 insert into VoteItem(Item,TitleID) values('VB.NET开发语言',2) 39 insert into VoteItem(Item,TitleID) values('Java开发语言',2) 40 insert into VoteItem(Item,TitleID) values('Visual Basic语言',2) 41 insert into VoteItem(Item,TitleID) values('PowerBuilder语言',2) 42 insert into VoteItem(Item,TitleID) values('Delphi语言',2) 43 insert into VoteItem(Item,TitleID) values('C++语言',2) 44 insert into VoteItem(Item,TitleID) values('C语言',2) 45 go 46 --编写一个触发器目的是当项目表的某个项目投票数加1时 47 --就让项目对应主题表的投票总数也加1 48 --每个主题投票总数在输出投票结果页面构造图形输出时要用 49 --也可以在程序中直接统计对应主题的投票总数,但是这种方法执行效率更高些 50 --特别是处理大量数据信息时,这种功能更实用 51 create trigger trigger_VoteItem 52 on VoteItem for update 53 as 54 begin 55 if update(Number)--当项目表的投票数更新时 56 begin 57 update VoteTitle set SumNumber=SumNumber+1 where TitleID=(select TitleID from inserted) 58 end 59 end--end trigger_VoteItem 60 go 61 select * from VoteTitle 62 select * from VoteItem 63 go