ASP.net开发标准控件之DropDownListAndListBox

一、 控件介绍

DropDownList 控件用于创建下拉列表。
DropDownList 控件中的每个可选项都是由 ListItem 元素定义的!

二、任务介绍

添加新网页DropDownListAndListBox.aspx,完成下图所示的功能:
这里写图片描述
(1) 当单击“提交”按钮后,会自动对下拉列表中显示为当前食物项累加统计出被选中的次数。
需要用到控件:1个DropDownList;1个ListBox;1个Button。

三、任务操作

3.1.完成基本界面

在S16解决方案的Web项目中添加新网页DropDownListAndListBox.aspx,并将控件:1个DropDownList;1个ListBox;1个Button拖到页面中,设置其属性值:
这里写图片描述

效果图如下:
设计图:
这里写图片描述
代码:
这里写图片描述

3.2 功能分析

1.功能描述与分析: 当单击“提交”按钮后,会自动对下拉列表中显示为当前食物项累加统计出被选中的次数。
此事件显然属于按钮的点击Click事件。
事件内容为:
双层循环下拉列表DropDownList的aItem和ListBox的Item,然后统计DropDownList原有次数,
判断aItem的Value和Item的Value相等且aItem选中,则改变两者的Value和ListBoxText的的Text属性值。
2.操作步骤:
1)打开Button的属性界面,选择事件图标,双击事件Click,创建了点击事件
这里写图片描述
2)添加点击事件内容:
这里写图片描述
2)运行页面:在“解决方案资源管理器”中选中DropDownListAndListBox.aspx页面,右击点击“在浏览器中查看”
效果图为:
这里写图片描述

原代码:

前端:DropDownListAndListBox.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownListAndListBox.aspx.cs" Inherits="Web.DropDownListAndListBox" %>

<!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:DropDownList ID="dpdlst" runat="server" AutoPostBack="True" 
           >
            <asp:ListItem Value="1.0">香蕉</asp:ListItem>
            <asp:ListItem Value="2.0">苹果</asp:ListItem>
            <asp:ListItem Value="3.0">梨子</asp:ListItem>
        </asp:DropDownList>

    </div>
    <asp:ListBox ID="lstbx" runat="server">
        <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:ListBox>
    <br />
    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="提交" />
    </form>
</body>
</html>

后台代码:DropDownListAndListBox.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 DropDownListAndListBox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            foreach (ListItem aItem in this.dpdlst.Items)
            {
                foreach (ListItem Item in this.lstbx.Items)
                {//求出上次被选中的次数。           
                    int iCount = Convert.ToInt32(
                    aItem.Value.Substring(aItem.Value.IndexOf('.') + 1));
                    //  对被选中的次数做累加,并按照“X.X”格式回写给列表项的“Value”属性。
                    if (aItem.Value == Item.Value && aItem.Selected)
                    {
                        aItem.Value = aItem.Value.Replace(
                        string.Format(".{0}", iCount),
                        string.Format(".{0}", iCount + 1));
                        Item.Value = Item.Value.Replace(
                        string.Format(".{0}", iCount),
                        string.Format(".{0}", iCount + 1));
                        //  修改列表项的“Text”属性,以便显示被选择的累加结果。
                        Item.Text = Item.Text.Replace(
                        string.Format("(已选择 {0} 次)", iCount),
                        string.Format("(已选择 {0} 次)", iCount + 1));
                    }
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值