一.相关控件及其任务实现
1. CheckBox + CheckBoxList(复选控件和复选组控件)
任务:
添加新网页CheckBoxAndCheckBoxList.aspx,完成以下的功能:
(1) 当“全部”复选框被选中的话,三个食物前面的复选框也都被选中。
(2) 当三个食物前面的复选框都被选中的话,“全部”复选框也自动被选中。
(3) 当三个食物前面的复选框没有全部被选中的话,“全部”复选框自动取消被选中。
(4) 单击“提交”按钮后,会自动按照选中状态累加统计出各个食物被选中的次数。
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web2
{
public partial class Checkboxlist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
//1.找到chkbxAll的选中状态state
bool chbxall = this.ChkbxAll.Checked;
//2.根据state来设定chkbxlst的选中状态
foreach (ListItem item1 in this.chkbxlst.Items)
{
item1.Selected = this.ChkbxAll.Checked;
}
}
protected void chkbxlst_SelectedIndexChanged(object sender, EventArgs e)
{
//1.统计checkboxlist的选中个数
int count = this.chkbxlst.Items.Cast<ListItem>().Count(p => p.Selected);
//2.比较checkboxlist的选中Items个数和checkboxlist的Items的个数是否相等,
//并将结果赋值给全选CheckBox
this.ChkbxAll.Checked = (count == this.chkbxlst.Items.Count);
}
protected void Button1_Click(object sender, EventArgs e)
{
int count;
foreach (ListItem item in chkbxlst.Items)
{
//1.判断当前Item是否被选中
if (item.Selected == true)
{
//2.获取上一次的点击次数
count = Convert.ToInt32(item.Value.Substring(item.Value.IndexOf('.') + 1));
item.Value = item.Value.Replace(string.Format(".{0}", count), string.Format(".{0}", count + 1));
item.Text = item.Text.Replace(string.Format("{0}", count), string.Format("{0}", count + 1));
}
}
}
}
}
2. RadioButton + RadioButtonList(单选控件和单选组控件)
任务:
添加新网页RadioButtonAndRadioButtonList.aspx,完成以下的功能:
(1) 当“取消”单选框被选中的话,三个食物前面的单选框都被取消选中。
(2) 单击“提交”按钮后,会自动按照选中状态累加统计出各个食物被选中的次数。
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web2
{
public partial class RadioButtonAndRadioButtonList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void rdobt_CheckedChanged(object sender, EventArgs e)
{
if (this.rdobt.Checked==true)//判断取消是否被选中
{
for (int i = 0; i < rdobtlst.Items.Count; i++)
{
rdobtlst.Items[i].Selected = false;//更改RadioList中的选中状态
this.rdobt.Checked = false;//更改取消选项的选中状态
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int count;
foreach (ListItem item in rdobtlst.Items)//遍历RdioList
{
//统计
count = Convert.ToInt32(item.Value.Substring(item.Value.IndexOf('.') + 1));
if (item.Selected == true)
{
item.Value = item.Value.Replace(string.Format(".{0}", count), string.Format(".{0}", count + 1));
item.Text = item.Text.Replace(string.Format("{0}", count), string.Format("{0}", count + 1));
}
}
}
}
}
3. BulletedList (用于创建一个无序或有序(编号)的项列表)
任务:
添加新网页BulletedList.aspx,完成以下的功能:
(1) 当单击某个食物的链接后,会自动对该食物累加统计出被选中的次数。
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web2
{
public partial class BulletedList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BulletedList1_Click(object sender, BulletedListEventArgs e)
{
ListItem blist = BulletedList1.Items[e.Index];//获取list的选项
int count = Convert.ToInt32(blist.Value.Substring(blist.Value.IndexOf('.') + 1));//求出上次被选中的次数
//对被选中的次数做累加,并按照“X.X”格式会写给列表项的“Value”属性;修改列表项的“Text”属性,一边显示 被选择的累加结果
blist.Value = blist.Value.Replace(string.Format(".{0}", count), string.Format(".{0}", count + 1));
blist.Text = blist.Text.Replace(string.Format("{0}", count), string.Format("{0}", count + 1));
}
}
}
4. DropDownList + ListBox(DropDownList用于创建下拉列表)
任务:
添加新网页DropDownListAndListBox.aspx,完成以下的功能:
(1) 当单击“提交”按钮后,会自动对下拉列表中显示为当前食物项累加统计出被选中的次数。
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web2
{
public partial class DropDownListAndListBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (ListItem item1 in this.DropDownList1.Items)//遍历DropDownList
{
foreach (ListItem item2 in this.ListBox1.Items)//遍历ListBox
{
int count = Convert.ToInt32(item1.Value.Substring(item1.Value.IndexOf('.') + 1));//统计上次被选中的次数
if(item1.Value == item2.Value && item1.Selected == true)//判断
{
item1.Value = item1.Value.Replace(string.Format(".{0}", count), string.Format(".{0}", count + 1));
item2.Value = item2.Value.Replace(string.Format(".{0}", count), string.Format(".{0}", count + 1));
item2.Text = item2.Text.Replace(string.Format("{0}", count), string.Format("{0}", count + 1));
}
}
}
}
}
}
5. MultiView (制作出选项卡的效果,MultiView 控件是一组 View 控件的容器。使用它可定义一组 View 控件,其中每个 View 控件都包含子控件)
任务:
添加新网页Multiview.aspx,完成以下的功能:
(1) 在该视图上实现对食物的选择。
(2) 当单击“提交”按钮后,会自动按照选中状态累加统计出各个食物被选中的次数,并跳转到下图所示的结果视图。
(3) 当单击“返回”按钮后,会回到实现对食物的选择视图。
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web2
{
public partial class Multiview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
this.MultiView1.ActiveViewIndex = 1;//切换视图到view2
//MultiView1.SetActiveView(View2);
foreach (ListItem itemck in this.CheckBoxList1.Items)
{
foreach (ListItem itemblt in BulletedList1.Items)
{
//获取上一次的点击次数
int count = Convert.ToInt32(itemck.Value.Substring(itemck.Value.IndexOf(".") + 1));
//判断checkboxlist的项是否被选中以及是否与BulletedList的项的值相等
if (itemck.Value == itemblt.Value && itemck.Selected)
{
itemck.Value = itemck.Value.Replace(string.Format(".{0}", count), string.Format(".{0}", count + 1));
itemblt.Value = itemblt.Value.Replace(string.Format(".{0}", count), string.Format(".{0}", count + 1));
itemblt.Text = itemblt.Text.Replace(string.Format("{0}", count), string.Format("{0}", count + 1));
}
}
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
this.MultiView1.ActiveViewIndex = 0;//切换视图到view1
//MultiView1.SetActiveView(View1);
}
}
}
6. Wizard (向导)
任务:
添加新网页Wizard.aspx,完成以下的功能:
(1) 在该视图上实现对食物的选择。
(2) 当单击“提交”按钮后,会自动按照选中状态累加统计出各个食物被选中的次数,并跳转到下图所示的结果视图。
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web2
{
public partial class Wizard : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
foreach (ListItem item in this.CheckBoxList1.Items)
{
foreach (ListItem item1 in this.BulletedList1.Items)
{
int count = Convert.ToInt32(item1.Value.Substring(item1.Value.IndexOf('.') + 1));
if (item.Selected == true && item1.Value==item.Value)
{
item1.Value = item1.Value.Replace(string.Format(".{0}", count), string.Format(".{0}", count + 1));
item.Value = item.Value.Replace(string.Format(".{0}", count), string.Format(".{0}", count + 1));
item1.Text = item1.Text.Replace(string.Format("{0}", count), string.Format("{0}", count + 1));
}
}
}
}
}
}