江湖救急。dropdownlist中的SelectedIndexChanged事件不可用是怎么回事啊?

guhonglangzi001 2010-07-29 09:41:24
要实现的功能是这样的在一个dropdownlist中选择施工单位,在第二个dropdownlist中显示与之对应的施工地点,且这两个控件都是绑定数据库的。也就是联动。

方案一与遇到的问题:
代码如下:
dropdownlist1:
<asp:Label ID="Label4" runat="server" Text="三违单位" CssClass="lable"></asp:Label> 
<asp:DropDownList ID="sdwmc" runat="server" CssClass="dropdownlist"
Width="100px" onselectedindexchanged="sdwmc_SelectedIndexChanged"
>
</asp:DropDownList>


dropdownlist2:
<asp:Label ID="lable_s" runat="server" Text="施工地点" CssClass="lable"></asp:Label> 
<asp:DropDownList ID="s_sgdd" runat="server" CssClass="dropdownlist"
onselectedindexchanged="s_sgdd_SelectedIndexChanged">
</asp:DropDownList>


cs文件:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Model;
using BLL;
using DAL;
using SqlHelpe;

namespace WebApplication1
{
public partial class sanw : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BLL_dwxx dwxx=new BLL_dwxx();
DataTable dt = new DataTable();
dt = dwxx.SelectAll();
//绑定施工单位
this.sdwmc.DataSource = dt;
this.sdwmc.DataTextField = "dwmc";
this.sdwmc.DataBind();




}
}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{

}

protected void TextBox1_TextChanged1(object sender, EventArgs e)
{

}

protected void s_sgdd_SelectedIndexChanged(object sender, EventArgs e)
{


}

protected void sdwmc_SelectedIndexChanged(object sender, EventArgs e)
{
//绑定施工地点
string sgdw = sdwmc.SelectedItem.Text;
BLL_dwddxx dwddxx = new BLL_dwddxx();
DataTable dt1 = new DataTable();
dt1 = dwddxx.selectdd(sgdw);
this.s_sgdd.DataSource = dt1;
this.s_sgdd.DataTextField = "sgdd";
this.s_sgdd.DataBind();
}

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{

}




}
}

其中可以确定:BLL_dwddxx dwddxx = new BLL_dwddxx();
DataTable dt1 = new DataTable();
dt1 = dwddxx.selectdd(sgdw);
this.s_sgdd.DataSource = dt1;
this.s_sgdd.DataTextField = "sgdd";
this.s_sgdd.DataBind();

绑定过程没有错误。


问题:当点击dropdownlist1时,dropdownlist2没反应,根据调试知道“protected void sdwmc_SelectedIndexChanged(object sender, EventArgs e)
”事件没执行,请问这是什么原因?,应该怎样解决。


方案二与遇到问题:
在dropdownlist1也就是“sdwmc”中加入属性“AutoPostBack="True" ”,也就是
<asp:Label ID="Label4" runat="server" Text="三违单位" CssClass="lable"></asp:Label> 
<asp:DropDownList ID="sdwmc" runat="server" CssClass="dropdownlist"
Width="100px" onselectedindexchanged="sdwmc_SelectedIndexChanged" AutoPostBack="True"
>
</asp:DropDownList>
并且在<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" AutoEventWireup="true" CodeBehind="sanw.aspx.cs" Inherits="WebApplication1.sanw" EnableEventValidation="false" %>
中加入EnableEventValidation="false",不然产生错误“回发或回调参数无效”

之后,在第二个dropdownlist(s_sgdd)中出现了与之相应的项,但问题是“无论怎样在第一个dropdownlist(sdwmc)中,只能选定第一个值(采煤一区),当选定第二个值(采煤二区)时,还是回到第一个值(采煤一区),好像是自动刷新了,又成了第一个值,”,请问这是什么原因?应该怎样解决?
另外请问,错误“回发或回调参数无效”除了]EnableEventValidation="false"之外,还有其它的方法吗?
有劳各位大侠了。
...全文
196 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xia411123129 2010-07-30
  • 打赏
  • 举报
回复
调试的时候 是否有取到值呢,用ViewState 保存的呢,是否有清空呢
周煜皓 2010-07-30
  • 打赏
  • 举报
回复
在需要出发 selectchanged 事件的 那个 dropdownlist 中启用 AutoPostBack 属性设置为true
guhonglangzi001 2010-07-30
  • 打赏
  • 举报
回复
设置了啊,AutoPostBack=true",但是设置之后发生错误?
错误代码:
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentException: 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。



问题:这个问题应该如何调试啊,除了EnableEventValidation="false",还有其它方法吗?
还有问题的关键是在第二个dropdownlist(s_sgdd)中出现了与之相应的项,但问题是“无论怎样在第一个dropdownlist(sdwmc)中,只能选定第一个值(采煤一区),当选定第二个值(采煤二区)时,还是回到第一个值(采煤一区),好像是自动刷新了,又成了第一个值,”,
这个问题怎么解决?
是不是跟
if (!IsPostBack)
{
BLL_dwxx dwxx=new BLL_dwxx();
DataTable dt = new DataTable();
dt = dwxx.SelectAll();
//绑定施工单位
this.sdwmc.DataSource = dt;
this.sdwmc.DataTextField = "dwmc";
this.sdwmc.DataBind();




}
有关。
mm51221 2010-07-30
  • 打赏
  • 举报
回复
难道!!!
一克代码 2010-07-30
  • 打赏
  • 举报
回复
AutoPostBack=true"
guhonglangzi001 2010-07-30
  • 打赏
  • 举报
回复
加入了啊,
如果不会传数据绑定dropdownlist(ssgdw),也就是第一个,回传的话不绑定

if (!IsPostBack)
{
BLL_dwxx dwxx=new BLL_dwxx();
DataTable dt = new DataTable();
dt = dwxx.SelectAll();
//绑定施工单位
this.sdwmc.DataSource = dt;
this.sdwmc.DataTextField = "dwmc";
this.sdwmc.DataBind();
}
myhope88 2010-07-30
  • 打赏
  • 举报
回复
设定AutoPostback为true才能及时触发事件,至于为啥回到第一个值,那是因为你的page_load中绑定数据时没有加入if(!IsPostBack)进行判断
guhonglangzi001 2010-07-30
  • 打赏
  • 举报
回复
方案二与遇到问题:
在dropdownlist1也就是“sdwmc”中加入属性“AutoPostBack="True" ”,也就是
<asp:Label ID="Label4" runat="server" Text="三违单位" CssClass="lable"></asp:Label> 
<asp:DropDownList ID="sdwmc" runat="server" CssClass="dropdownlist"
Width="100px" onselectedindexchanged="sdwmc_SelectedIndexChanged" AutoPostBack="True"
>
</asp:DropDownList>
并且在<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" AutoEventWireup="true" CodeBehind="sanw.aspx.cs" Inherits="WebApplication1.sanw" EnableEventValidation="false" %>
中加入EnableEventValidation="false",不然产生错误“回发或回调参数无效”

之后,在第二个dropdownlist(s_sgdd)中出现了与之相应的项,但问题是“无论怎样在第一个dropdownlist(sdwmc)中,只能选定第一个值(采煤一区),当选定第二个值(采煤二区)时,还是回到第一个值(采煤一区),好像是自动刷新了,又成了第一个值,”,请问这是什么原因?应该怎样解决?
另外请问,错误“回发或回调参数无效”除了]EnableEventValidation="false"之外,还有其它的方法吗?
有劳各位大侠了。
微工程 2010-07-29
  • 打赏
  • 举报
回复
刚才没看内容就顶 了
这是可以回发了,SelectedIndexChanged事件的执行要设AutoPostBack属性外
要使二者关联,那么在其中一个事件中执行后,要绑定第二个dropdownlist的数据
  • 打赏
  • 举报
回复
ChaoYang0502 2010-07-29
  • 打赏
  • 举报
回复
应该是因为验证控件的问题,先去掉页面中的验证控件,AutoPostBack = "true" 试试事件是否触发
polarissky 2010-07-29
  • 打赏
  • 举报
回复
关键是AutoPostBack = "true"
duxj007 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyq11 的回复:]
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlLB" runat……
[/Quote]
hehe~~~~
amandag 2010-07-29
  • 打赏
  • 举报
回复
关键是AutoPostBack = "true"
wuyq11 2010-07-29
  • 打赏
  • 举报
回复
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlLB" runat="server" Width="15%" AutoPostBack="True" OnSelectedIndexChanged="ddlLB_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddlChild" runat="server" Width="20%">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
if (!IsPostBack)
{BindDll();}
protected void ddlLB_SelectedIndexChanged(object sender, EventArgs e)
{
if(this.ddlLB.SelectedValue!=null)
{
BindChild(this.ddlLB.SelectedValue);
}
}
没问题
wuyq11 2010-07-29
  • 打赏
  • 举报
回复
<asp:DropDownList ID="sdwmc" runat="server" CssClass="dropdownlist" AutoPostBack=true"
Width="100px" onselectedindexchanged="sdwmc_SelectedIndexChanged"
>
</asp:DropDownList>
string sgdw = sdwmc.SelectedValue;
设置EnableEventValidation
有IsPostabck不会
兔子-顾问 2010-07-29
  • 打赏
  • 举报
回复
好像是PostBack属性。。。
兔子-顾问 2010-07-29
  • 打赏
  • 举报
回复
AutoPostBack属性设置为true没?

62,243

社区成员

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

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

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

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