|
|
| 用ASP.NET 2.0设计网络在线投票系统(4) |
| 作者:不详 来源:不详 发布时间:2006-8-14 10:23:41 发布人:chinazhan |
减小字体
增大字体
投票页面设计 在应用程序WebVote中添加一个新的Web页面,并命名为WebOnlineVote.ASPx,它的代码隐藏文件为WebOnlineVote.aspx.cs文件。 1.页面设计 在页面WebOnlineVote.aspx上添加一个数据网格控件、两个Button控件和一个Label控件,它们的名称分别为VoteList、 VoteBtn、ShowVote和VoteMessage。控件VoteList用来显示参与投票的所有项目;控件VoteBtn提交用户的投票;控件 ShowVote实现用户查看投票情况;控件VoteMessage显示用户投票的操作结果。页面WebOnlinVote.aspx的设计界面如图6所示。  图6 页面WebOnlinVote.aspx的设计界面 页面WebOnlinVote.aspx的HTML设计代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebOnlinVote.aspx.cs" Inherits="WebOnlinVote" %> <HTML><HEAD><title>网络在线投票系统</title></HEAD> <asp:DataGrid id="VoteList" CssClass="GbText" Runat="server" AutoGenerateColumns="False" DataKeyField="VoteID"> <Columns> <asp:TemplateColumn ItemStyle-Width="200"> <ItemTemplate><%# DataBinder.Eval(Container.DataItem,"Item")%> </ItemTemplate></asp:TemplateColumn> <asp:TemplateColumn ItemStyle-Width="100"> <ItemTemplate> <asp:CheckBox ID="VoteCheck" Runat="server"></asp:CheckBox> </ItemTemplate></asp:TemplateColumn> </Columns> <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <ItemStyle BackColor="White" ForeColor="#330099" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> </asp:datagrid> <asp:button id="VoteBtn" Runat="server" Width="100" Text="我要投票"></asp:button> <asp:button id="ShowVote" Runat="server" Width="100" Text="查看投票"></asp:button> <asp:Label ID="VoteMessage" Runat="server" Visible="False" ForeColor="red" Font-Bold="True">投票成功!!!</asp:Label></td> </HTML> 1.页面初始化 页面WebOnlinVote.aspx调用函数Page_Load(Object sender,EventArgs e)初始化,该函数调用函数BindVoteListData()从数据库投票表Votes中获取所有投票项目的信息,并把获取的数据设置为数据网格控件VoteList的数据源。函数Page_Load(Object sender,EventArgs e)和函数BindVoteListData()的程序代码如下: private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { //绑定投票的项目 BindVoteListData(); VoteMessage.Visible = false; } } private void BindVoteListData() { //获取所有数据 WebVote.Vote vote = new Vote(); SqlDataReader recv = vote.GetVotes(); //设置控件的数据源,并绑定数据 VoteList.DataSource = recv; VoteList.DataBind(); recv.Close(); //关闭数据读取器 } 网络在线投票系统运行之后,投票页面WebOnlinVote.aspx的初始化界面如图7所示,此时显示被投票的项目信息。  图7 投票页面WebOnlinVote.aspx的初始化界面 2.投票功能 用户单击页面 WebOnlinVote.aspx中的【我要投票】按钮和【查看投票】按钮分别触发事件VoteBtn_Click(object sender, System.EventArgs e)和事件ShowVote_Click(object sender, System.EventArgs e),它们分别实现用户投票功能和查看投票功能。在投票事件中,事件首先检查用户对哪些项目进行了投票,然后更改项目的票数。在查看投票事件中,事件重定向到页面ShowVoteInfo.aspx。事件VoteBtn_Click(object sender, System.EventArgs e)和事件ShowVote_Click(object sender, System.EventArgs e)的程序代码如下: private void VoteBtn_Click(object sender, System.EventArgs e) { //定义类 WebVote.Vote vote = new Vote(); try { //添加用户的投票的项目 foreach(DataGridItem item in VoteList.Items) { //查找每个投票项目的选择控件 CheckBox check = (CheckBox)item.FindControl("VoteCheck"); if(check != null) { //说明用户已经投票,则需要添加这一票 if(check.Checked == true) { //修改数据库中的票数 vote.UpdateVote(Int32.Parse( VoteList.DataKeys[item.ItemIndex].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", " ")); } } private void ShowVote_Click(object sender, System.EventArgs e) { //导向查看投票结果页面 Response.Redirect("~/ShowVoteInfo.aspx"); } 做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。
|
| |
|
[]
[返回上一页]
[打 印]
[收 藏] |
|
| ∷相关文章评论∷ (评论内容只代表网友观点,与本站立场无关!) [更多评论...] |
|
|