//文章作者:叶茂(猫猫)(本学习笔记系列文章拒绝任何形式的修改、加工或转载)
这个投票系统的大部分源码来自马军主编的《精通ASP.NET 2.0 网络应用系统开发》,但是按照原文提供的代码,系统是无法在VS2005下编译通过的。鉴于此,猫重写了部分代码使其能正常运行于VS2005,现贴出来给大家分享。
1、系统功能设计
- 投票选项管理
- 对选项投票
- 投票情况查看
2、数据库设计
- VoteDB
3、存储过程设计
-
添加投票选项
-
- CREATE PROCEDURE Proc_AddVote
- @Item varchar(100)
- AS
- INSERT INTO
- Votes(Item,VoteCount)
- VALUES(@Item,0)
- RETURN @@Identity
- GO
-
删除投票选项
-
- CREATE PROCEDURE Proc_DeleteVote
- @VoteID int
- AS
- DELETE Votes
- WHERE VoteID = @VoteID
- GO
-
更新投票选项
-
- CREATE PROCEDURE Proc_UpdateVote
- @VoteID int
- AS
- UPDATE Votes
- SET VoteCount = VoteCount + 1
- WHERE VoteID = @VoteID
- GO
-
显示所有投票项目
- CREATE PROCEDURE Pr_GetVotes
- AS
- SELECT *
- FROM Votes
- Order By VoteID
- GO
4、数据库访问层设计
在App_Code文件夹下添加一个.cs类文件用来封装对以上存储过程的调用方法。
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using SQLHelper;
- using System.Data.SqlClient;
- namespace WebVote
- {
- /// <summary>
- /// Summary description for Vote
- /// </summary>
- public class Vote
- {
- public SqlDataReader GetVotes()
- {
- ///定义类SQLHelper
- SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
- ///定义保存从数据库获取的结果的DataReader
- SqlDataReader dr = null;
- try
- {
- ///执行存储过程
- sqlHelper.RunProc(“Proc_GetVotes”, out dr);
- }
- catch (Exception ex)
- {
- ///抛出执行数据库异常
- SystemError.CreateErrorLog(ex.Message);
- throw new Exception(ex.Message, ex);
- }
- ///返回从数据库获取的结果
- return (dr);
- }
- public int AddVote(String sItem)
- {
- ///定义类SQLHelper
- SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
- ///创建访问数据库的参数
- SqlParameter[] paramList = {
- sqlHelper.CreateInParam(“@Item”, SqlDbType.VarChar,100,sItem)
- };
- try
- {
- ///执行存储过程
- return (sqlHelper.RunProc(“Proc_AddVote”, paramList));
- }
- catch (Exception ex)
- {
- ///抛出执行数据库异常
- SystemError.CreateErrorLog(ex.Message);
- throw new Exception(ex.Message, ex);
- }
- }
- public void UpdateVote(int nVoteID)
- {
- ///定义类SQLHelper
- SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
- ///创建访问数据库的参数
- SqlParameter[] paramList = {
- sqlHelper.CreateInParam(“@VoteID”, SqlDbType.Int, 4,nVoteID)
- };
- try
- {
- ///执行存储过程
- sqlHelper.RunProc(“Proc_UpdateVote”, paramList);
- }
- catch (Exception ex)
- {
- ///抛出执行数据库异常
- SystemError.CreateErrorLog(ex.Message);
- throw new Exception(ex.Message, ex);
- }
- }
- public void DeleteVote(int nVoteID)
- {
- ///定义类SQLHelper
- SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
- ///创建访问数据库的参数
- SqlParameter[] paramList = {
- sqlHelper.CreateInParam(“@VoteID”, SqlDbType.Int, 4,nVoteID)
- };
- try
- {
- ///执行存储过程
- sqlHelper.RunProc(“Proc_DeleteVote”, paramList);
- }
- catch (Exception ex)
- {
- ///抛出执行数据库异常
- SystemError.CreateErrorLog(ex.Message);
- throw new Exception(ex.Message, ex);
- }
- }
- }
- }
对于数据访问层,需要使用一个SQLHelper的DLL文件(下载地址:http://d.download.csdn.net/down/661742/zhaobisha 将其粘贴到Bin文件夹中。
5、程序页面设计
-
主页面(Default.aspx)
-
- …
- <form id=“form1” runat=“server”>
- <div>
- <asp:LinkButton ID=“ItemManageLink” runat=“server” BackColor=“Black” ForeColor=“White”
- Height=“24px” Width=“149px” PostBackUrl=“VoteItemManage.aspx”>投票选项管理</asp:LinkButton><br />
- <br />
- <asp:LinkButton ID=“OnlineVoteLink” runat=“server” BackColor=“Black” ForeColor=“White”
- Height=“24px” Width=“149px” PostBackUrl=“~/OnlineVote.aspx”>在线投票</asp:LinkButton> <br />
- <br />
- <asp:LinkButton ID=“ViewVoteLink” runat=“server” BackColor=“Black” ForeColor=“White”
- Height=“24px” Width=“149px” PostBackUrl=“~/ShowVoteResult.aspx”>查看投票结果</asp:LinkButton></div>
- </form>
- …
-
投票选项管理页面(VoteItemManage.aspx)
-
- …
- <form id=“form1” runat=“server”>
- <div>
- <asp:ListBox ID=“ItemList” runat=“server” Rows=“10” Width=“150px”></asp:ListBox>
- <asp:Button ID=“deleteBtn” runat=“server” CommandName=“delete” OnClientClick=“deleteBtn_Click”
- Text=“删除此项” OnClick=“deleteBtn_Click” /> <br />
- <asp:TextBox ID=“Item” runat=“server” Height=“18px” Width=“290px”></asp:TextBox>
- <asp:Button ID=“AddBtn” runat=“server” Text=“增加新选项” OnClick=“AddBtn_Click” /></div>
- </form>
- …
-
在线投票页面(OnlineVote.aspx)
-
- …
- <form id=“form1” runat=“server”>
- <div>
- <asp:GridView ID=“VoteList” runat=“server” AutoGenerateColumns=“False” BackColor=“White” BorderColor=“#CC9966” BorderStyle=“None” BorderWidth=“1px” CellPadding=“4” DataKeyNames=“VoteID”>
- <Columns>
- <asp:TemplateField HeaderText=“勾选” SortExpression=“VoteID”>
- <ItemTemplate>
- <asp:CheckBox ID=“VoteCheck” runat=“server” />
- </ItemTemplate>
- <ItemStyle HorizontalAlign=“Center” />
- </asp:TemplateField>
- <asp:BoundField DataField=“Item” HeaderText=“投票选项” SortExpression=“Item” >
- <ControlStyle BackColor=“#E0E0E0” />
- <ItemStyle Width=“200px” HorizontalAlign=“Center” />
- </asp:BoundField>
- <asp:BoundField DataField=“VoteID” ShowHeader=“False” SortExpression=“VoteID” Visible=“False” />
- </Columns>
- <FooterStyle BackColor=“#FFFFCC” ForeColor=“#330099” />
- <PagerStyle BackColor=“#FFFFCC” ForeColor=“#330099” HorizontalAlign=“Center” />
- <HeaderStyle BackColor=“#990000” Font-Bold=“True” ForeColor=“#FFFFCC” />
- <RowStyle BackColor=“White” ForeColor=“#330099” />
- <SelectedRowStyle BackColor=“#FFCC66” Font-Bold=“True” ForeColor=“#663399” />
- </asp:GridView>
- </div>
- <asp:Button ID=“VoteBtn” runat=“server” Text=“我要投票” OnClick=“VoteBtn_Click” />
- <asp:Button ID=“ShowVote” runat=“server” Text=“查看结果” PostBackUrl=“~/ShowVoteResult.aspx” />
- <asp:Label ID=“VoteMessage” runat=“server”>投票成功!</asp:Label><br />
- <br />
- </form>
- …
-
查看投票结果页面(ShowVoteResult.aspx)
- …
- <form id=“form1” runat=“server”>
- <div>
- <asp:GridView ID=“VoteList” runat=“server” BackColor=“White” BorderColor=“#CC9966”
- BorderStyle=“None” BorderWidth=“1px” CellPadding=“4” AutoGenerateColumns=“False”>
- <FooterStyle BackColor=“#FFFFCC” ForeColor=“#330099” />
- <RowStyle BackColor=“White” ForeColor=“#330099” />
- <SelectedRowStyle BackColor=“#FFCC66” Font-Bold=“True” ForeColor=“#663399” />
- <PagerStyle BackColor=“#FFFFCC” ForeColor=“#330099” HorizontalAlign=“Center” />
- <HeaderStyle BackColor=“#990000” Font-Bold=“True” ForeColor=“#FFFFCC” />
- <Columns>
- <asp:BoundField DataField=“Item” HeaderText=“投票项目” SortExpression=“Item” />
- <asp:TemplateField HeaderText=“所占总票的百分比”>
- <ItemStyle Width=“300px” />
- <ItemTemplate>
- <asp:Image ID=“VoteDegree” runat=“server” Width=“” Height=“20px” ImageUrl=“~/Images/Vote.gif” />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField DataField=“VoteCount” HeaderText=“票数” SortExpression=“VoteCount” />
- </Columns>
- </asp:GridView>
- <asp:Label ID=“VoteMessage” runat=“server” Text=“Label”></asp:Label> <asp:Button
- ID=“Button1” runat=“server” PostBackUrl=“~/OnlineVote.aspx” Text=“返回继续投票” /><br />
- </div>
- </form>
- …
6、页面功能设计
- 投票选项管理(VoteItemManage.aspx.cs)
-
- …
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- BindVoteListData();
- }
- }
- protected void BindVoteListData()
- {
- WebVote.Vote vote = new Vote();
- SqlDataReader rs = vote.GetVotes();
- ItemList.DataTextField = “Item”;
- ItemList.DataValueField = “VoteID”;
- ItemList.DataSource = rs;
- ItemList.DataBind();
- rs.Close();
- }
- protected void AddBtn_Click(object sender, EventArgs e)
- {
- if (Item.Text.Length > 0)
- {
- WebVote.Vote vote = new Vote();
- try
- {
- vote.AddVote(Item.Text.Trim());
- BindVoteListData();
- Response.Write(“<script>alert(‘” + ASPNET2System.OPERATIONADDSUCCESSMESSAGE + “‘)</script>”);
- }
- catch (Exception ex)
- {
- Response.Redirect(“~/DesktopModules/ErrorPage.aspx?ErrorUrl=” + ASPNET2System.RedirectErrorUrl(Request.RawUrl) + “&ErrorMessage=” + ex.Message.Replace(“/n”, “”));
- }
- }
- }
- protected void deleteBtn_Click(object sender, EventArgs e)
- {
- if (ItemList.SelectedIndex <= -1)
- {
- Response.Write(“<script>alert(‘” + ASPNET2System.OPERATIONNOSELECTMESSAGE + “‘)</script>”);
- return;
- }
- WebVote.Vote vote = new Vote();
- try
- {
- vote.DeleteVote(int.Parse(ItemList.SelectedValue));
- BindVoteListData();
- }
- catch (Exception ex)
- {
- Response.Redirect(“~/DesktopModules/ErrorPage.aspx?ErrorUrl=” + ASPNET2System.RedirectErrorUrl(Request.RawUrl) + “&ErrorMessage=” + ex.Message.Replace(“/n”, “”));
- }
- }
- …
- 在线投票(OnlineVote.aspx.cs)
-
- …
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- BindVoteListData();
- VoteMessage.Visible = false;
- }
- }
- private void BindVoteListData()
- {
- WebVote.Vote vote = new Vote();
- SqlDataReader rs = vote.GetVotes();
- VoteList.DataSource = rs;
- VoteList.DataBind();
- rs.Close();
- }
- protected void VoteBtn_Click(object sender, EventArgs e)
- {
- WebVote.Vote vote = new Vote();
- try
- {
- //for (int i = 0; i <= VoteList.Rows.Count – 1; i++)
- foreach (GridViewRow row in VoteList.Rows)
- {
- CheckBox chk = (CheckBox)row.FindControl(“VoteCheck”);
- if (chk != null)
- {
- if (chk.Checked == true)
- {
- vote.UpdateVote(int.Parse(VoteList.DataKeys[row.RowIndex].Value.ToString()));
- VoteMessage.Visible = true;
- }
- }
- }
- Response.Write(“<script>window.alert(‘感谢投票!’)</script>”);
- }
- catch (Exception ex)
- {
- Response.Redirect(“~/DesktopModules/ErrorPage.aspx?ErrorUrl=” + ASPNET2System.RedirectErrorUrl(Request.RawUrl) + “&ErrorMessage=” + ex.Message.Replace(“/n”, “”));
- }
- }
- …
- 查看投票结果(ShowVoteResult.aspx.cs)
- …
- int voteTotal = 0;
- protected void Page_Load(object sender, EventArgs e)
- {
- SetVoteTotal();
- if (!Page.IsPostBack)
- {
- BindVoteListData();
- ShowImageDegree();
- VoteMessage.Text = “总票数:” + voteTotal.ToString() + “票”;
- }
- }
- protected void BindVoteListData()
- {
- WebVote.Vote vote = new Vote();
- SqlDataReader rs = vote.GetVotes();
- VoteList.DataSource = rs;
- VoteList.DataBind();
- rs.Close();
- }
- protected void SetVoteTotal()
- {
- WebVote.Vote vote = new Vote();
- SqlDataReader rs = vote.GetVotes();
- voteTotal = 0;
- while (rs.Read())
- {
- voteTotal += int.Parse(rs[“voteCount”].ToString());
- }
- rs.Close();
- }
- protected void ShowImageDegree()
- {
- foreach (GridViewRow row in VoteList.Rows)
- {
- Image img = (Image)row.FindControl(“VoteDegree”);
- if (img != null)
- {
- //vote.UpdateVote(Int32.Parse(VoteList.DataKeys[row.RowIndex].Value.ToString()));
- string dw = VoteList.Rows[row.RowIndex].Cells[2].Text.ToString();
- img.Width = FormatVoteImage(FormatVoteCount(dw));
- }
- }
- }
- public int FormatVoteCount(string voteCount)
- {
- if (voteCount.Length <= 0)
- {
- return (0);
- }
- if (voteTotal > 0)
- {
- return (int.Parse(voteCount) * 100 / voteTotal);
- }
- return (0);
- }
- public int FormatVoteImage(int voteCount)
- {
- return (voteCount * 3);
- }
- …
7、小结
SQLHelper以及Gridview控件的使用为本系统实现的主要手段。其中,Gridview控件中的遍历数据格的代码是最基础也是最需要掌握的内容。
今天的文章【ASP.NET2.0学习笔记】开发投票系统分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/33068.html