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


  来源:网络搜集 | 作者:不详 | 浏览: | 发送给好友 | 添加到收藏夹

投票项目管理页面设计

  在应用程序WebVote中添加一个新的Web页面,并命名为VoteItemManage.aspx,它的代码隐藏文件为VoteItemManage.aspx.cs文件。

  1.页面设计

  在页面VoteItemManage.aspx上添加一个列表控件、一个Button控件、一个TextBox控件和一个ImageButton控件,它们的名称分别为ItemList、AddBtn、Item和deleteBtn。控件ItemList显示投票项目表中的所有数据;控件AddBtn实现添加一个新的投票项目;控件Item用来输入新的投票项目名称;控件deleteBtn删除一个投票项目。页面ItemManage.aspx的设计界面如图4所示。

图4 页面VoteItemManage.aspx的设计界面

  页面VoteItemManage.aspx的HTML设计代码如下:

<title>网络在线投票系统</title>

<link href="CSS/ASPNET2BaseCss.css" type="text/css" rel="stylesheet">

<asp:ListBox id="ItemList" width="150" rows="10" runat="server"

CssClass="SelectSta" />

<asp:ImageButton id="deleteBtn" ImageUrl="~/images/delete.gif"

AlternateText="删除此项" runat="server"

CommandName="delete" OnClick="deleteBtn_Click" />

<asp:TextBox ID="Item" Runat="server" Width="252"

CssClass="InputCss"></asp:TextBox>

<asp:Button ID="AddBtn" Runat="server" Text="增加新的投票项目"

CssClass="ButtonCss" OnClick="AddBtn_Click"></asp:Button>
  2.页面初始化

  页面VoteItemManage.aspx调用函数Page_Load(Object sender,EventArgs e)初始化,该函数调用函数BindVoteListData()从数据库投票表Votes中获取所有投票的项目,并把获取的数据绑定到列表控件ItemList。函数Page_Load(Object sender,EventArgs e)和函数BindVoteListData()的程序代码如下:
private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{ //绑定投票项目列表的数据

BindVoteListData();

}

}

private void BindVoteListData()

{ //获取投票项目的所有数据

WebVote.Vote vote = new Vote();

SqlDataReader recv = vote.GetVotes();

//设置列表控件的Text属性和Value属性

ItemList.DataTextField = "Item";

ItemList.DataValueField = "VoteID";

//设置控件的数据源,并绑定控件的数据

ItemList.DataSource = recv;

ItemList.DataBind();

recv.Close(); //关闭数据读取器

}
  网络在线投票系统运行之后,投票项目管理页面VoteItemManage.aspx的初始化界面如图5所示,此时已经显示投票的项目信息。

图5 投票项目管理页面VoteItemManage.aspx的初始化界面

  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", " "));

}

}

本新闻共5页,当前在第3页   上一页 下一页12345

上一篇
闪动论坛 打印此页 发送给好友 返回顶部