DropDownList1的值不能重复

qaz020 2009-11-17 04:00:30
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


if (!Page.IsPostBack)
{


ListItem l1 = new ListItem();
l1.Value = "1";
l1.Text = "flow1";
DropDownList1.Items.Add(l1);

ListItem l2 = new ListItem();
l2.Value = "1";
l2.Text = "flow2";
DropDownList1.Items.Add(l2);

ListItem l3 = new ListItem();
l3.Value = "1";
l3.Text = "flow3";
DropDownList1.Items.Add(l3);

ListItem l4 = new ListItem();
l4.Value = "1";
l4.Text = "flow4";
DropDownList1.Items.Add(l4);

ListItem l5 = new ListItem();
l5.Value = "1";
l5.Text = "flow5";
DropDownList1.Items.Add(l5);

ListItem l6 = new ListItem();
l6.Value = "1";
l6.Text = "flow6";
DropDownList1.Items.Add(l6);

ListItem l7 = new ListItem();
l7.Value = "8";
l7.Text = "flow7";
DropDownList1.Items.Add(l7);

ListItem l8 = new ListItem();
l8.Value = "11";
l8.Text = "flow8";
DropDownList1.Items.Add(l8);
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedItem.Text;
}

当 DropDownList1选择的值是 flow1 到 flow6 时,显示的值为空,真不知为什么
只有选择 flow7 flow8 Label1.Text 的值才会显示

...全文
122 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen_ya_ping 2009-11-17
  • 打赏
  • 举报
回复
value这个属性让它唯一
zhengliyan_star 2009-11-17
  • 打赏
  • 举报
回复
value 不能相同吧!
sh_suyuelin 2009-11-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ivws_19 的回复:]
ListItem.value不能重复
dropdownlist是根据value来区分item的
[/Quote]
非常正确
添加项
仿佛这样方便

for (int i = 0; i < 7; i++)
{
this.DropDownList1.Items.Insert(i, new ListItem("flow" + (i+1), i.ToString()));

}
ivws_19 2009-11-17
  • 打赏
  • 举报
回复
要用唯一的字段来做value
ivws_19 2009-11-17
  • 打赏
  • 举报
回复
ListItem.value不能重复
dropdownlist是根据value来区分item的
xuyang09 2009-11-17
  • 打赏
  • 举报
回复
同意楼上的说法
duck530 2009-11-17
  • 打赏
  • 举报
回复
建议不要把VALUE用一样哟
其实在内部是用VALUE在取好像
手抓宝 2009-11-17
  • 打赏
  • 举报
回复
如果在实际开发中遇到这样的问题,只能说明你的详细设计有问题,不如说下你的需求让大家帮看看
qaz020 2009-11-17
  • 打赏
  • 举报
回复
换成Label1.Text=DropDownList1.selectedValue; 故障依旧



把 flow1 到 flow6 的值改为 不重复就可以了

但是在实际开发中可能值会重复,不如何处理
沉默味道ron 2009-11-17
  • 打赏
  • 举报
回复
顶!
PandaIT 2009-11-17
  • 打赏
  • 举报
回复
换成Label1.Text=DropDownList1.selectedValue;
baoxuetianxia 2009-11-17
  • 打赏
  • 举报
回复
烦人呢啊。

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧