当前位置:编程学习 > asp >>

投票系统分享

前几天完成老师的作业,网上投票系统,今天终于做完了,特地把代码分享一下。
第一个页面代码:
  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        

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,