投票系统-显示所有的投票主题(进行投票)

1,本投票系统通过博客Id获取用户Id,再通过用户ID获取投票主题

 

界面如下:

 

 

 

后台代码如下:

 

代码
 1 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="AllVote.ascx.cs" Inherits="AllVote" %>
 2 <table style="width:800px; height:400px"   cellspacing="0">
 3     <tr>
 4        
 5         <td style="width:189px; background-image: url(image/login_1.gif);">
 6         </td>
 7          <td style="background-image: url(image/login_2.gif)">
 8            <table style="width:400px;  height:400px;">
 9             <tr >
10                 <td style="width:100px;"></td>
11                 <td style="width:275px;"></td>
12                  <td style="width:100px;"></td>
13             </tr>
14              <tr >
15                 <td style="width:100px;"></td>
16                 <td style="width:275px; text-align: center;">
17                     <span style="font-size: 16pt; color: #66ff00">查看所有投票</span></td>
18                  <td style="width:100px;"></td>
19             </tr>
20              <tr >
21                 <td style="width:100px;"></td>
22                 <td style="width:275px;"></td>
23                  <td style="width:100px;">
24                      <asp:ImageButton ID="returnIndex" runat="server" ImageUrl="~/image/ruturnIndex.gif" OnClick="returnIndex_Click" /></td>
25             </tr>
26              <tr >
27                 <td style="width:100px; height: 68px;"></td>
28                 <td style="width:275px; height: 68px;">
29                     <span style="color: #33cc00">以下列出的是全部投票主题,选择你感兴趣的进行投票吧!</span></td>
30                  <td style="width:100px; height: 68px;"></td>
31             </tr>
32              <tr >
33                 <td style="width:100px; height: 34px;"></td>
34                 <td style="width:275px; height: 34px;"></td>
35                  <td style="width:100px; height: 34px;"></td>
36             </tr>
37              <tr >
38                 <td style="width:100px; height: 34px;"></td>
39                 <td style="width:275px;" rowspan="5">
40                     <asp:DataList ID="DL" runat="server">
41                          <ItemTemplate>
42                             <asp:HyperLink ID="HplinkVoteTitle" runat="server" NavigateUrl='<%# "~/Vote.aspx?Voteid="+DataBinder.Eval(Container.DataItem,"VoteItem_id") %>' Text='<%# DataBinder.Eval(Container.DataItem,"VoteItem") %>' ForeColor="#C04000"></asp:HyperLink>
43                         </ItemTemplate>
44                         <ItemStyle ForeColor="#C04000" />
45                     </asp:DataList></td>
46                  <td style="width:100px; height: 34px;"></td>
47             </tr>
48              <tr >
49                 <td style="width:100px;"></td>
50                  <td style="width:100px;"></td>
51             </tr>
52              <tr >
53                 <td style="width:100px;"></td>
54                  <td style="width:100px;"></td>
55             </tr>
56              <tr >
57                 <td style="width:100px;"></td>
58                  <td style="width:100px;"></td>
59             </tr>
60              <tr >
61                 <td style="width:100px; height: 27px;"></td>
62                  <td style="width:100px; height: 27px;"></td>
63             </tr>
64              <tr >
65                 <td colspan="3">
66                     <table>
67                     <tr>
68                        <td style="width: 96px; height: 21px">共有 
69                            <asp:Label ID="labCount" runat="server" Text=" "></asp:Label></td>
70                        <td style="width: 97px; height: 21px;"> 当前<asp:Label ID="labNowPage" runat="server" Text="1"></asp:Label></td>
71                        <td style="width: 15px; height: 21px">
72                            <asp:LinkButton ID="lnkbtnFirst" runat="server" Width="47px" OnClick="lnkbtnFirst_Click">首页</asp:LinkButton></td>
73                        <td style="width: 70px; height: 21px;"> <asp:LinkButton ID="lnkbtnFront" runat="server" OnClick="lnkbtnFront_Click">上一页</asp:LinkButton></td>
74                        <td style="width: 74px; height: 21px;"> <asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton></td>
75                        <td style="width: 126px; height: 21px;"> <asp:LinkButton ID="lnkbtnLast" runat="server" Width="33px" OnClick="lnkbtnLast_Click"> 尾页</asp:LinkButton></td>
76                        
77                     </tr>
78                   
79                   </table>
80                 </td>
81             </tr>
82            </table>
83          
84          
85          
86          
87         </td>
88          <td style="width:200px; background-image: url(image/login_3.gif);">
89          </td>
90     </tr>
91    
92 </table>
93 

 

 

 

 

 

。cs后台代码

 

代码
  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 
 12 public partial class AllVote : System.Web.UI.UserControl
 13 {
 14     BaseDb obj = new BaseDb();
 15 
 16     protected void Page_Load(object sender, EventArgs e)
 17     {
 18         if (!IsPostBack)
 19         {
 20             Bind();
 21         }
 22 
 23     }
 24 
 25     #region  获得博客ID
 26     public int GetBlogID()
 27     {
 28         int BlogID =3;
 29         return BlogID;
 30     }
 31 
 32     #endregion
 33 
 34     #region 获得用户ID
 35     public int GetUserID()
 36     {
 37         int userID = 0;
 38         int BlogId = GetBlogID();
 39         try
 40         {        //根据博客ID获取用户ID
 41             DataSet ds = obj.getdatabydataset("select User_id from BlogManage where Blog_id=" + "'" + BlogId + "'");
 42 
 43             userID = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
 44 
 45         }
 46         catch (Exception ee)
 47         {
 48             Response.Write("<script>alert('" + ee.Message + "');</script>");
 49         }
 50 
 51         return userID;
 52     }
 53 
 54     #endregion
 55 
 56     public void Bind()
 57     {
 58         int CurrentPage = Convert.ToInt32(labNowPage.Text);
 59         PagedDataSource ps = new PagedDataSource();//生产PagedDataSource的实例  主要是实现datalist分页
 60         int userID = GetUserID();
 61         DataSet ds = obj.getdatabydataset("select VoteItem_id,VoteItem from VoteItem where User_id = " + "'" + userID + "'"  );
 62         ps.DataSource = ds.Tables[0].DefaultView;
 63         ps.AllowPaging = true;
 64         ps.PageSize =5;
 65         ps.CurrentPageIndex = CurrentPage - 1;//取得当前页面的页码
 66         lnkbtnFirst.Enabled = true;
 67         lnkbtnFront.Enabled = true;
 68         lnkbtnNext.Enabled = true;
 69         lnkbtnLast.Enabled = true;
 70         if (CurrentPage == 1)
 71         {
 72             lnkbtnFirst.Enabled = false;//不显示首页按钮
 73             lnkbtnFront.Enabled = false;//不显示第上一页按钮
 74         }
 75         if (CurrentPage == ps.PageCount)
 76         {
 77             lnkbtnLast.Enabled = false;//不显示最后一页按钮
 78             lnkbtnNext.Enabled = false;//不显示下一页按钮
 79         }
 80         labCount.Text = Convert.ToString(ps.PageCount);
 81         DL.DataSource = ps;
 82         DL.DataBind();
 83     }
 84 
 85     protected void returnIndex_Click(object sender, ImageClickEventArgs e)
 86     {
 87         Response.Redirect("~/VoteIndex.aspx");
 88     }
 89     protected void lnkbtnFirst_Click(object sender, EventArgs e)   //单击首页时,显示当前页为第一页
 90     {
 91         labNowPage.Text = "1";
 92         Bind();
 93     }
 94     protected void lnkbtnFront_Click(object sender, EventArgs e)  //单击上一页时,当前页减1
 95     {
 96         labNowPage.Text = Convert.ToString(Convert.ToInt32(labNowPage.Text)-1);
 97         Bind();
 98     }
 99     protected void lnkbtnNext_Click(object sender, EventArgs e)  //单击下一页,当前页加1
100     {
101         labNowPage.Text = Convert.ToString(Convert.ToInt32(labNowPage.Text) + 1);
102         Bind();
103     }
104     protected void lnkbtnLast_Click(object sender, EventArgs e)
105     {
106         labNowPage.Text = labCount.Text;
107         Bind();
108     }
109 }
110 

 

 

 

选择投票选项:

界面如下:

 

 

 

前台代码

 

 

代码
  1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Vote.aspx.cs" Inherits="Vote" %>
  2 
  3  
  4 
  5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6 
  7 <html xmlns="http://www.w3.org/1999/xhtml" >
  8 <head runat="server">
  9     <title>无标题页</title>
 10 </head>
 11 <body>
 12     <form id="form1" runat="server">
 13     <div>
 14         <table style="width:800px; height:400px"   cellspacing="0">
 15     <tr>
 16        
 17         <td style="width:189px; background-image: url(image/login_1.gif);">
 18         </td>
 19          <td style="background-image: url(image/login_2.gif)">
 20            <table style="width:400px;  height:400px;">
 21             <tr >
 22                 <td style="width:100px;"></td>
 23                 <td style="width:200px;"></td>
 24                  <td style="width:100px;"></td>
 25             </tr>
 26              <tr >
 27                 <td style="width:100px;"></td>
 28                 <td style="width:200px;"></td>
 29                  <td style="width:100px;"></td>
 30             </tr>
 31              <tr >
 32                 <td style="width:100px; height: 32px;"></td>
 33                 <td style="width:200px; height: 32px;"></td>
 34                  <td style="width:100px; height: 32px;">
 35                      <asp:ImageButton ID="returnIndex" runat="server" ImageUrl="~/image/ruturnIndex.gif" OnClick="returnIndex_Click" /></td>
 36             </tr>
 37              <tr >
 38                 <td style="width:100px;">
 39                     </td>
 40                 <td style="width:200px;">
 41                     &nbsp;还等什么!赶快投票吧!</td>
 42                  <td style="width:100px;">
 43                      </td>
 44             </tr>
 45              <tr >
 46                 <td style="width:100px; height: 34px;">
 47                     
 48                     </td>
 49                 <td style="width:200px; height: 34px;"><asp:Label ID="lbvoteTitle" runat="server" Text="Label"></asp:Label>
 50                     </td>
 51                  <td style="width:100px; height: 34px;"></td>
 52             </tr>
 53              <tr >
 54                 <td style="width:100px; height: 34px;"></td>
 55                 
 56                  
 57                 <td style="width:200px; height: 34px;">
 58                     <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
 59                     </asp:CheckBoxList>
 60                     </td>
 61                  <td style="width:100px; height: 34px;"></td>
 62             </tr>
 63              <tr >
 64                 <td style="width:100px;"></td>
 65                 <td style="width:200px;">
 66                     <asp:Label ID="checkresult" runat="server"></asp:Label>
 67                 </td>
 68                  <td style="width:100px;"></td>
 69             </tr>
 70              <tr >
 71                 <td style="width:100px;"></td>
 72                 <td style="width:200px;">
 73                       <table>
 74                       <tr>
 75                          <td style="width: 139px; height: 39px;">
 76                              <asp:ImageButton ID="imbVote" runat="server" ImageUrl="~/image/Vote.gif" OnClick="imbVote_Click" />
 77                          </td>
 78                          <td style="width: 174px; height: 39px;">
 79                           <asp:ImageButton ID="imbResult" runat="server" ImageUrl="~/image/checkresult.gif" OnClick="imbResult_Click" />
 80                          </td>
 81                      
 82                       </tr>
 83                      
 84                    </table>
 85                 
 86                 </td>
 87                  <td style="width:100px;"></td>
 88             </tr>
 89              <tr >
 90                 <td style="width:100px;"></td>
 91                 <td style="width:200px;"></td>
 92                  <td style="width:100px;"></td>
 93             </tr>
 94              <tr >
 95                 <td style="width:100px;"></td>
 96                 <td style="width:200px;"></td>
 97                  <td style="width:100px;"></td>
 98             </tr>
 99              <tr >
100                 <td style="width:100px;"></td>
101                 <td style="width:200px;"></td>
102                  <td style="width:100px;"></td>
103             </tr>
104            </table>
105          
106          
107          
108          
109         </td>
110          <td style="width:200px; background-image: url(image/login_3.gif);">
111          </td>
112     </tr>
113    
114 </table></div>
115     </form>
116 </body>
117 </html>
118 

 

 

 

.cs后台代码,实现过程主要通过投票Id和投票项进行读取票数

 

 

 

代码
 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 
12 public partial class Vote : System.Web.UI.Page
13 {
14     BaseDb obj = new BaseDb();
15   static  int VoteItemID = 0;
16     protected void Page_Load(object sender, EventArgs e)
17     {
18        VoteItemID = Convert.ToInt32(Request["Voteid"]);
19         if (!IsPostBack)
20         {
21            
22             labBind();
23             cblBind();
24         }
25 
26 
27     }
28 
29     #region  对投票主题进行绑定
30     public void labBind()
31     {
32         DataSet ds = obj.getdatabydataset("select voteItem from VoteItem where VoteItem_id = " + "'" + VoteItemID + "'");
33         lbvoteTitle.Text = ds.Tables[0].Rows[0][0].ToString();
34     }
35 
36     #endregion
37 
38     #region
39 
40     public void cblBind()
41     {
42         DataSet ds = obj.getdatabydataset("select VoteSelect_id,Voteselect from VoteSelect where VoteItem_id = " + "'" + VoteItemID + "'");
43         CheckBoxList1.DataSource = ds;
44         CheckBoxList1.DataTextField = "VoteSelect";
45         CheckBoxList1.DataValueField = "VoteSelect_id";
46         CheckBoxList1.DataBind();
47 
48     }
49     #endregion
50     protected void returnIndex_Click(object sender, ImageClickEventArgs e)
51     {
52         Response.Redirect("~/VoteIndex.aspx");
53     }
54     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
55     {
56         int index;
57         string checkedrusult = "你已经选择了:";
58         for (index = 0; index < CheckBoxList1.Items.Count; index++)
59         {
60             if (CheckBoxList1.Items[index].Selected)
61             {
62                 checkedrusult +=" " +CheckBoxList1.Items[index].Text;
63 
64             }
65             checkresult.Text = checkedrusult;
66         }
67             
68 
69     }
70     protected void imbVote_Click(object sender, ImageClickEventArgs e)
71     {
72         int VoteSelectid = 0;//获取被选择的投票项ID
73         int index ;//索引
74 
75         for (index =0; index < CheckBoxList1.Items.Count; index++)
76         {
77             if (CheckBoxList1.Items[index].Selected)
78             {
79                 VoteSelectid = Convert.ToInt32(CheckBoxList1.Items[index].Value.ToString());
80                 obj.ExecuteSql("update VoteSelect set  VoteCount = VoteCount+1 where VoteSelect_id = " + VoteSelectid + " " + "and" + " " + "VoteItem_id=" + VoteItemID);
81               
82             }
83         }
84 
85     }
86     protected void imbResult_Click(object sender, ImageClickEventArgs e)
87     {
88         Response.Redirect("~/VoteResult.aspx?VoteID="+VoteItemID );
89     }
90 }
91 

 

 

3投票结果

后台代码:如下:

 

代码
 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 
12 public partial class VoteResult : System.Web.UI.UserControl
13 {
14     int VoteItemID =0;
15     BaseDb obj = new BaseDb();
16     protected void Page_Load(object sender, EventArgs e)
17     {
18         VoteItemID = Convert.ToInt32(Request["VoteID"]);
19         Response.Write(GetResult());
20 
21     }
22 
23     #region 获取当前投票主题的总票数
24 
25     public decimal GetAllCount()
26     {
27       decimal TotalCount = 0;//总票数;
28       int index;
29 
30         //DataSet ds = obj.getdatabydataset("SELECT sum(VoteCount)   FROM VoteSelect where VoteItem_id = " + VoteItemID   );
31         DataSet ds = obj.getdatabydataset( " Select VoteCount from VoteSelect where VoteItem_id = " + VoteItemID);
32         for (index = 0; index < ds.Tables[0].Rows.Count; index++)
33         {
34             TotalCount += Convert.ToDecimal(ds.Tables[0].Rows[index][0].ToString());
35         }
36 
37            
38  
39         return TotalCount; 
40     }
41 #endregion
42 
43    
44 
45     #region
46     public string GetResult()
47     {
48        
49         decimal VoteCountPercent;//获取数据的百分比
50         decimal VoteCounts;//获取数据的票数
51         string VoteSelects;//获得投票项
52         int index;
53         string   strBody = "<body style=text-align:center><table width=60% border=1 cellpadding=0 cellspacing=0 text-align:center>\n";
54         string strPercent;
55         DataSet ds = obj.getdatabydataset("select VoteSelect,VoteCount from VoteSelect where VoteItem_id = " + VoteItemID);
56       decimal TotalCounts = GetAllCount();
57  
58         for (index = 0; index < ds.Tables[0].Rows.Count; index++)
59         {
60             VoteSelects  = ds.Tables[0].Rows[index][0].ToString();
61             VoteCounts  = Convert.ToDecimal(ds.Tables[0].Rows[index][1]);
62           VoteCountPercent  = GetPercent(VoteCounts ,TotalCounts )*100;
63        
64             strPercent = VoteCountPercent.ToString();
65             if (strPercent.Length > 5)  //如果百分比的长度大于5的话,截取 为00.00
66             {
67                 strPercent = strPercent.Substring(0,5);
68             }
69             strBody += "<tr><td width=100 style=font-size:9pt;text-align:center>" +  VoteSelects  + "</td><td width=50 style=font-size:9pt;color=red;text-align:center>" + VoteCounts + "票</td><td style=font-size:9pt><img src=image/bar1.gif height=10 width=" + strPercent + "%>" + strPercent + "%</td></tr>\n";
70         }
71         strBody += "</table></body>";
72         return strBody;
73 
74     }
75     #endregion
76 
77     #region 获得百分比
78     public decimal GetPercent(decimal VoteCount, decimal TotalCounts)
79     {
80         if (TotalCounts == 0)
81         {
82             TotalCounts++;//防止除0
83 
84         }
85 
86         return VoteCount / TotalCounts;
87     }
88 
89 
90     #endregion
91 
92 }
93 

 

 

以上是投票系统的第二部分,以下是我 的一些经验。

 

 

投票系统碰到的问题:

问题1:用户id的 获取?

先通过博客id获得用户id

 

页面布局:用表格

问题二:

单击添加时,出现如下错误:

解决方法:原因是数据库中的表名不一致。              

数据库内容不一样:

         

 

 两个表的VoteItem_id  不一致。为什么?

这两个表的

原因为,在获取投票主题Id的时候,函数如下:

public int AutoID(string tableName, string colName)

    {

int voteValueID = 0;

               string     voteValueID1= "";

        try

        {

            DataSet ds = obj.getdatabydataset("select Max(" + colName + ")from" + " " + tableName);

            voteValueID1 = ds.Tables[0].Rows[0][0].ToString();

        }

        catch (Exception ee)

        {

            Response.Write("<script>alert('" + ee.Message + "');</script>");

        }

        Response.Write(voteValueID1);

        return voteValueID;

}

问题出现在红字体缺少空格/导致数据读不出来、

数据库语句问题:

obj.ExecuteSql("delete from VoteSelect where VoteSelect=" +"'" + voteSelect + "'");

删除某一行是一定要写成如上格式。最重要的是蓝色字体不能没有。

还有若sql语句有字符串变量时,一定要给他加“‘” 变量 +“’”  不然运行不了。

布局问题:

 

控件之间无法对齐

删除数据项时数据显示不出来,的一个原因是找不到用户Id

总结:编程时,要注意各中细节。

问题:代码执行后VoteCount出现重复累加;

for (index =0; index < CheckBoxList1.Items.Count; index++)

        {

            VoteSelectid =Convert.ToInt32(CheckBoxList1.SelectedValue.ToString());有问题。

            obj.ExecuteSql("update VoteSelect set  VoteCount = VoteCount+1 where VoteSelect_id = " +VoteSelectid +" "+ "and" +" " + "VoteItem_id=" + VoteItemID );

        }

解决方法,多加一个判断条件:

          if (CheckBoxList1.Items[index].Selected)

            {

                VoteSelectid = Convert.ToInt32(CheckBoxList1.Items[index].Value.ToString());

                obj.ExecuteSql("update VoteSelect set  VoteCount = VoteCount+1 where VoteSelect_id = " + VoteSelectid + " " + "and" + " " + "VoteItem_id=" + VoteItemID);

}

在进行插入数据项是,要同时把投票项的票数置0。不然将会出现投票数不会自动增加如图:

解决方法:在插入投票选项时,同时置投票项的票数置0

出现上面的原因:

 另外的解决办法是将投票的票数全部取出来,然后再进行累加。

posted on 2010-04-11 22:42  wtq  阅读(1238)  评论(0编辑  收藏  举报