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

2008-02-22 09:28:01来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折



  3.添加功能

  单击页面VoteItemManage.aspx中的【增加新的投票项目】按钮,触发事件AddBtn_Click(object sender, System.EventArgs e),该事件实现添加一个新的投票项目。事件AddBtn_Click(object sender, System.EventArgs e)的程序代码如下:

private void AddBtn_Click(object sender, System.EventArgs e)
{
if (Item.Text.Length > 0)
{ //定义类
WebVote.Vote vote = new Vote();
try
{ //添加新数据项
vote.AddVote(Item.Text.Trim());
BindVoteListData();
//显示操作结果信息
Response.Write("<script>window.alert('"
ASPNET2System.OPERATIONADDSUCCESSMESSAGE "')</script>");
}

catch (Exception ex)
{ //显示添加操作中的失败、错误信息
Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
ASPNET2System.RedirectErrorUrl(Request.RawUrl)
"&ErrorMessage=" ex.Message.Replace("\n", " "));
}
}
}

  4.删除功能

  单击页面VoteItemManage.aspx中的【×】按钮,触发事件deleteBtn_Click(object sender, System.EventArgs e),该事件实现删除已选择的投票项目。事件deleteBtn_Click(object sender, System.EventArgs e)的程序代码如下:

protected void deleteBtn_Click(object sender, ImageClickEventArgs e)

{

if (ItemList.SelectedIndex <= -1)

{ //显示操作结果信息

Response.Write("<script>window.alert('"

ASPNET2System.OPERATIONNOSELECTMESSAGE "')</script>");

return;

}

//定义类

WebVote.Vote vote = new Vote();

try

{ //删除数据

vote.DeleteVote(Int32.Parse(ItemList.SelectedValue));

//重新绑定数据

BindVoteListData();

}

catch (Exception ex)

{ //显示删除操作中的失败、错误信息

Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="

ASPNET2System.RedirectErrorUrl(Request.RawUrl)

"&ErrorMessage=" ex.Message.Replace("\n", " "));

}

}

  投票页面设计

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:如何在删除并重新安装 IIS 之后修复 IIS 映射

下一篇:如何建立自己的新闻发布系统?