updatepanel里面的dropdownlist的问题

gistop 2010-01-27 01:39:41
环境:vs2008 C#
不光是dropdownlist,像listbox也存在这样的问题
情况是:updatepanel1里面有dropdownlist1和dropdownlist2
dropdownlist1的items由另一个updatepanel2里面的按钮控件通过代码生成datatable绑定而成.
在dropdownlist1的dropdownlist1_SelectedIndexChanged事件下控制dropdownlist2的显示,
可是dropdownlist1有的项能选中,并触发dropdownlist1_SelectedIndexChanged事件,有的item选中后自动跳到另一个没选的item上去,比如我明明选中index为10的,可在调试时发现selectedindex成了2了,在这样的情况下,很多项目选中后的selectedindex都指向同一个值,比如2,就根本不触发dropdownlist1_SelectedIndexChanged事件了.望高人指点

注:设定autopostback属性为true了

...全文
202 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gistop 2010-01-29
  • 打赏
  • 举报
回复
恩,感谢各位的回答.问题解决了,是 大他datatable的问题,datatable有一列数据有重复的,注意绑定时别有重复的行数据就行了,谢谢各位
happyboyxq 2010-01-28
  • 打赏
  • 举报
回复
程序中绑定的字段是否有问题。
xray2005 2010-01-27
  • 打赏
  • 举报
回复
如果是同一个页面,你可以就直接用一个updatepanel 就可以了。

-------------------------------------------------------
可是dropdownlist1有的项能选中,并触发dropdownlist1_SelectedIndexChanged事件,有的item选中后自动跳到另一个没选的item上去,比如我明明选中index为10的,可在调试时发现selectedindex成了2了,
------------------------------------------
是不是代码里自己进行了判断处理。导致选中的项不一样?
polarissky 2010-01-27
  • 打赏
  • 举报
回复
楼主,把你的代码贴出来,大家看看哪里问题?
polarissky 2010-01-27
  • 打赏
  • 举报
回复

按照楼主的意图,做个DEMO,正常执行啊。我贴DEMO给你:
Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</ContentTemplate>

</asp:UpdatePanel>
</div>
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Button ID="Button" runat="server" Text="Button" onclick="Button_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

Default.aspx.cs:

using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button_Click(object sender, EventArgs e)
{
List<string> items = new List<string>();
items.Add("请选择...");
items.Add("祖国");
items.Add("万岁");
for (int i = 0; i < items.Count; i++)
{
this.DropDownList1.Items.Add(new ListItem(items[i]));
}
this.DropDownList1.SelectedIndex = 0;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = this.DropDownList1.SelectedIndex;
string textValue = "你选择了DropDownList1的Index是:" + selectedIndex;
ListItem item = new ListItem(textValue);
if (!DropDownList2.Items.Contains(item))
{
DropDownList2.Items.Add(item);
}
DropDownList2.Text = textValue;
}
}

mzjmicrosoft 2010-01-27
  • 打赏
  • 举报
回复
贴代码

谷歌:dropdownlist无刷新二级联动
xingshungames 2010-01-27
  • 打赏
  • 举报
回复
没碰到过。帮你顶
bychgh 2010-01-27
  • 打赏
  • 举报
回复
帮顶
dd__dd 2010-01-27
  • 打赏
  • 举报
回复
楼主想做联动去网上搜一下AJAX的联动就可以了
三碗猪脚 2010-01-27
  • 打赏
  • 举报
回复
看下 赵老师的 ASP.NET AJAX深入浅出系列课程 的第2课和第3课,专门讲updatepannel

注意:回传的属性

不是所有的控件都能在updatepannel中使用的
wangan2008 2010-01-27
  • 打赏
  • 举报
回复
,,,,注释几行 看看效果
Ezy_DK 2010-01-27
  • 打赏
  • 举报
回复
代码看看

62,075

社区成员

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

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

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

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