| 搜索频道 | 源码下载 | 站长代码论坛 | 文章分类 | 最新专题 | 源码交易 | 加入收藏
首页|资讯|图形图像|网站开发|程序设计|数据库|多媒体|机械电子|办公系列|路由技术|原理|应用|考试|系统
文章搜索:
 您的位置:首页网站开发ASP.NETAsp.Net基础教程 → 用ASP.NET 2.0设计网络在线投票系统
用ASP.NET 2.0设计网络在线投票系统

日期:2006-8-10 10:10:29 人气:     [ ]
上一页 [1] [2] [3] [4] [5] 下一页
  投票页面设计

  在应用程序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");

}

上一页 [1] [2] [3] [4] [5] 下一页
出处:本站原创 作者:zzcode
 相关软件
·小雅调查投票系统
·宇航通用投票系统 V1.0 TEST版
·长江大学校徽评选投票系统
·长江大学校徽评选投票系统
·心情网络投票系统
·零距离投票系统VoteZ v1.5
·雪晖在线投票系统 2005
·蓝滨投票系统c# v1.0 Beta
·雪晖在线投票系统 v2.3 元旦版
·UO投票系统 v2.1 单项目多投版
 

 热点文章

·用ASP.NET 2.0设计网络..
·AutoPostBack 属性及P..
·ASP.NET 2.0的页面缓存..
·Calendar Web 控件的事..
·ASP.NET 2.0中创建内容..
·ASP.NET2.0数据库入门..
·ListItem Web 控件
·ASP.NET+MySQL:强势组..
·ASP.net简介
·RegularExpressionVal..
·取得客户端浏览器的信..
·ASP.NET2.0 数据绑定函..
·ASP.NET 2.0中XSLT的使..
·ASP.NET环境的安装(1)..
·分页功能

 推荐文章

·端午非物质文化遗产登..
·动态网站Web开发PHP、..
·PS绘中秋佳节的一轮明..
·细谈网页优化和网站优..
·建站常识:如何使用FT..
·如何快速建造一个成功..
·ASP.NET 2.0 中的创建..
·ASP.NET2.0服务器控件..
·在ASP.NET应用中插入f..
·用Photoshop打造逼真立..
·Windows 2003搭建虚拟..
·站长必读:Web创业的1..
·如何测试机房的速度和..
·北京奥运体育图标发布..
·网络视频广告将身价百..