ASP.Net的标准控件--20180124

一.相关控件及其任务实现

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)   当单击“提交”按钮后,会自动按照选中状态累加统计出各个食物被选中的次数,并跳转到下图所示的结果视图。

(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 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));
                    }
                }
            }
        }
    }
}

二. 小Tip!
       每个列表项的Value属性的格式规定为“X.X”,“.”前的X为序号,“.”后的X为被选中的累加次数。
       对于CheckBox、CheckBoxList、ListBox、RadioButton、RadioButtonList 这几个控件的AutoPostBack属性设置为True,AutoPostBack意思是自动回传,设置为True也就是说此控件值更改后和服务器进行交互。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值