加入收藏 - 网站地图 - 网站搜索 -
阅读新闻

用ASP.NET 2.0设计网络在线投票系统

[日期:09-20] [字体: ]
投票页面设计

  在应用程序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的初始化界面


上一页 [1] [2] [3] [4] [5] 下一页   
 
评论 】 【 推荐 】 【 打印
上一篇:VS2005和ASP.NET2.0中使用强类型数据
下一篇:Asp.net中的页面乱码的问题
相关新闻