这个BUG是有关两个DropDownList互动产生的问题,请看前台页面代码,很简单,一个Form,两行FormRow,各有一个DropDownList,一个是商品类别,一个商品名称,如下:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="EmptyProjectNet40_FineUI.Test" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <f:PageManager ID="PageManager1" runat="server" />
- <f:Form ID="Form2" runat="server" ShowBorder="false" ShowHeader="false" Width="200px">
- <Rows>
- <f:FormRow ID="FormRow1" runat="server">
- <Items>
- <f:DropDownList ID="ddlLB" runat="server" Label="商品类别" AutoPostBack="true" OnSelectedIndexChanged="ddlLB_SelectedIndexChanged"></f:DropDownList>
- </Items>
- </f:FormRow>
- <f:FormRow ID="FormRow2" runat="server">
- <Items>
- <f:DropDownList ID="ddlSP" runat="server" Label="商品名称"></f:DropDownList>
- </Items>
- </f:FormRow>
- </Rows>
- </f:Form>
- </form>
- </body>
- </html>
复制代码 后台代码也很简单,定义了一个公用类SP,有4个属性:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using FineUI;
- namespace EmptyProjectNet40_FineUI
- {
- public class SP
- {
- public int SPID { get; set; } //商品ID
- public string SPLB { get; set; } //商品类别
- public int SPSL { get; set; } //商品数量
- public string SPMC { get; set; } //商品名称
- }
- public partial class Test : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- FillSPLB();
- FillSPMC();
- }
- }
- private List<SP> GetListSP()
- {
- List<SP> list = new List<SP>();
- list.Add(new SP { SPID = 1, SPLB = "蔬菜", SPMC = "白菜", SPSL = 10 });
- list.Add(new SP { SPID = 2, SPLB = "蔬菜", SPMC = "青菜", SPSL = 20 });
- list.Add(new SP { SPID = 3, SPLB = "蔬菜", SPMC = "萝卜", SPSL = 30 });
- list.Add(new SP { SPID = 4, SPLB = "蔬菜", SPMC = "青椒", SPSL = 40 });
- list.Add(new SP { SPID = 5, SPLB = "蔬菜", SPMC = "黄瓜", SPSL = 50 });
- list.Add(new SP { SPID = 6, SPLB = "水果", SPMC = "苹果", SPSL = 10 });
- list.Add(new SP { SPID = 7, SPLB = "水果", SPMC = "香蕉", SPSL = 20 });
- list.Add(new SP { SPID = 8, SPLB = "水果", SPMC = "梨子", SPSL = 30 });
- list.Add(new SP { SPID = 9, SPLB = "水果", SPMC = "桃子", SPSL = 40 });
- list.Add(new SP { SPID = 10, SPLB = "水果", SPMC = "杨梅", SPSL = 50 });
- return list;
- }
- private void FillSPLB()
- {
- var linq = GetListSP().Select(t => t.SPLB).Distinct();
- ddlLB.Items.Clear();
- foreach (var item in linq)
- {
- ddlLB.Items.Add(new ListItem(item.ToString(), item.ToString()));
- }
- }
- private void FillSPMC()
- {
- string _SPLB = this.ddlLB.SelectedValue;
- List<SP> list = GetListSP().Where(t => t.SPLB == _SPLB).ToList<SP>();
- this.ddlSP.Items.Clear();
- foreach (SP sp in list)
- {
- //这样添加就出现问题
- ddlSP.Items.Add(new ListItem(sp.SPMC, sp.SPSL.ToString()));
- //这样添加就没有问题
- //ddlSP.Items.Add(new ListItem(sp.SPMC, sp.ID.ToString()));
- }
- ddlSP.SelectedIndex = 0;
- }
- protected void ddlLB_SelectedIndexChanged(object sender, EventArgs e)
- {
- FillSPMC();
- Alert.ShowInTop(ddlSP.SelectedItem.Text);
- }
- }
- }
复制代码
问题来了,如果这样添加商品名称项:ddlSP.Items.Add(new ListItem(sp.SPMC, sp.SPSL.ToString()));
注意,这里的商品数量SPSL在原始的list中不同实例有重复,即有数量均为10的商品,则会发生如下情况
页面首次打开,如下图:
点击商品类别,选择“水果”,正常情况下,应该是商品名称DropDownList所有项目清空,并添加了全部商品类别为“水果”的商品,同时由于我默认选中了第一个,则应该会弹出对话框并显示“苹果”,问题是,为什么商品名称第一个显示是“白菜”,但是弹出对话框是“苹果”呢?如下图:
我们改变策略,默认选中第二个商品名称,即FillSPMC()中最后一行改成ddlSPMC.SelectedIndex=1;结果...依然不行,如下图:
我们再改变策略,页面加载后,先手工选择第二个商品名称,然后再选择商品类别“水果”,OYeah,终于正确了,如下图:
我已经用空项目重现了BUG,三石兄请帮忙看看,Test.aspx是用FINEUI做的,有问题;Test2.aspx是用ASP.NET原生控件做的,木有问题。
|