一、任务描述
添加新网页RadioButtonAndRadioButtonList.aspx,完成下图所示的功能:
(1) 当“取消”单选框被选中的话,三个食物前面的单选框都被取消选中。
(2) 单击“提交”按钮后,会自动按照选中状态累加统计出各个食物被选中的次数。
需要用到控件:1个RadioButton;1个RadioButtonList;1个Button。
二、操作
2.1.完成基本界面
在S16解决方案的Web项目中添加新网页RadioButtonAndRadioButtonList.aspx,并将控件:1个RadioButton;1个RadioButtonList;1个Button拖到页面中,并分别修改属性值
RadioButtonList的Item:
效果图如下:
设计图:
代码:
2.2 功能一
1功能描述:. 当“取消”单选框被选中的话,三个食物前面的单选框都被取消选中。
2 功能分析:
首先分析这是谁的事件,很明显是属于“取消”单选框的事件,即其选中状态改变时发生
事件内容为:
若““取消”单选框选中,则单选框列表所有项都是未选中的;
3操作步骤:
1)打开RadioButton的属性(F4),选择事件,双击CheckedChange事件,创建单选框选中改变事件
2)写事件内容:
3)运行效果:
运行页面:在“解决方案资源管理器”中选中RadioButtonAndRadioButtonList.aspx页面,右击点击“在浏览器中查看”
2.3 功能二
1.功能描述: 单击“提交”按钮后,会自动按照选中状态累加统计出各个食物被选中的次数。
2.功能分析:
此事件显然属于按钮的点击事件
事件内容:
因为单选框列表是个集合,应当循环每个列表项进行判断:
首先获取原有的选中次数,其次判断此次有无选中,选中则+1
3.操作步骤:
1)打开按钮button的属性界面,选择事件图标,双击事件Click,创建了按钮点击事件
3)写事件内容:
4)运行页面:在“解决方案资源管理器”中选中RadioButtonAndRadioButtonList.aspx页面,右击点击“在浏览器中查看”
效果图为:
三、总结:
实现功能时一定要分析清楚,这属于事件吗?哪个控件的事件?什么事件?然后理清楚逻辑,分析事件内容应该是怎样的,最后能用文字叙述出其步骤,那么接下来要做的就是将步骤翻译成C#代码语言即可。
对于RadioButton和RadioButtonList控件,需要特别说明的是记得将其AutoPostBack属性设置为true,即单击时,会回发到服务器,这样才会响应到事件。
源码:
前端:RadioButtonAndRadioButtonList.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadioButtonAndRadioButtonList.aspx.cs" Inherits="Web.RadioButtonAndRadioButtonList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
你喜欢下列哪些食物?<asp:RadioButton ID="rdobtnCancel" runat="server" AutoPostBack="True"
oncheckedchanged="rdbtnCancel_CheckedChanged" Text="取消" />
</div>
<asp:RadioButtonList ID="rdobtnlst" runat="server" AutoPostBack="True" >
<asp:ListItem Value="1.0">香蕉(已选择 0 次)</asp:ListItem>
<asp:ListItem Value="2.0">苹果(已选择 0 次)</asp:ListItem>
<asp:ListItem Value="3.0">梨子(已选择 0 次)</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="提交" />
</form>
</body>
</html>
后端:RadioButtonAndRadioButtonList.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web
{
public partial class RadioButtonAndRadioButtonList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void rdbtnCancel_CheckedChanged(object sender, EventArgs e)
{
if (rdobtnCancel.Checked)//若“取消”选中
{
//设置所有项状态为未选中
foreach (ListItem item in rdobtnlst.Items)
{ item.Selected = false; }
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (ListItem aItem in this.rdobtnlst.Items)
{//求出上次被选中的次数。
int iCount = Convert.ToInt32(
aItem.Value.Substring(aItem.Value.IndexOf('.') + ));
// 对被选中的次数做累加,并按照“X.X”格式回写给列表项的“Value”属性。
if (aItem.Selected)
{
aItem.Value = aItem.Value.Replace(
string.Format(".{0}", iCount),
string.Format(".{0}", iCount + ));
aItem.Text = aItem.Text.Replace(
string.Format("(已选择 {0} 次)", iCount),
string.Format("(已选择 {0} 次)", iCount + ));
}
}
}
}
}