CascadingDropDown 错误 - DropDownList 有一个无效的 selectedValue

发布于 2024-10-19 17:24:24 字数 477 浏览 3 评论 0原文

我有级联下拉菜单,通过 jquery ajax 填充。点击链接获取示例代码 下载示例代码

导致异常的步骤:

1:选择“型号年份”、“品牌”和“型号”

2:单击“清除按钮”重置下拉列表,此时会引发异常。

我怎样才能摆脱这个错误?我有一种奇怪的方法来规避它,通过在清除单击时设置隐藏字段等等,但是我会在页面上有很多其他控件来进行回发,而我的解决方法变得越来越笨拙,我正在寻找一个真正的解决方案。

可能我做错了什么,但欢迎任何指点。

我对链接进行了一些访问更改。请尝试让我知道这是否有效。

编辑: 该示例是使用 VS2005 / .NET 2.0 和 WinXP 开发的,但服务器将是 Win2003。

I have Cascading Dropdowns, populated via jquery ajax. Follow the link to get the sample code
Download Sample Code

Steps that results into an exception:

1: Make selection for ModelYear, Make and Model

2: Click "Clear button" to reset the dropdowns and that's when it throws an exception.

How can I get rid of this error? I have a weird way to circumvent it by setting hiddenfield upon Clear click and what not, but I will have lot many other controls on page that will do a postback and the workaround I have is getting clumsy and I am looking for a real solution.

May be I am doing something wrong but any pointers welcome.

I have made some access changes to the link. Please try and let me know if that works.

Edit:
The sample is developed using VS2005 / .NET 2.0 and WinXP but server will be Win2003.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七婞 2024-10-26 17:24:24

我已经检查了您的代码,我认为一个快速且简单的修复方法是检查请求是否来自清除按钮。基本上,如果您按下了清除按钮,我不会调用 ReloadDropDownSelection() 。

public partial class CascadingUC : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ModelYears.Attributes.Add("onchange", "OnModelYearChange()");
            Makes.Attributes.Add("onchange", "OnMakeChange()");
            Models.Attributes.Add("onchange", "OnModelChange()");
            LoadMakes();
            LoadModels();
        }
        else
        {
            if ((Request.Form["btnClear"]==null)||(Request.Form["btnClear"] != "Clear"))
                ReloadDropDownSelection();
        }
    }

正如我在评论中告诉你的,我认为这里的问题是视图状态。您在服务器端加载组合 (PopulateDropDownList),然后在客户端更改项目。

我花了几年时间与这些东西(asp.net、viewstate、数千个事件)作斗争,当 ASP.NET MVC 出现时,我看到了曙光;-)
我真的非常讨厌 ASP.NET Webforms。我认为这几乎是一个笑话。

I've checked your code and I think a fast and easy fix would be to check if the request comes from the clear button. Basically I wouldn't call ReloadDropDownSelection() if you've pressed the clear button.

public partial class CascadingUC : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ModelYears.Attributes.Add("onchange", "OnModelYearChange()");
            Makes.Attributes.Add("onchange", "OnMakeChange()");
            Models.Attributes.Add("onchange", "OnModelChange()");
            LoadMakes();
            LoadModels();
        }
        else
        {
            if ((Request.Form["btnClear"]==null)||(Request.Form["btnClear"] != "Clear"))
                ReloadDropDownSelection();
        }
    }

As I told you in my comments I think the problem here is the viewstate. You load your combo server-side (PopulateDropDownList) and then change the items client-side.

I spent a few years struggling with these stuff (asp.net, viewstate, thousands of events) and when ASP.NET MVC came out I've seen the light ;-)
I've really really hated ASP.NET webforms. I think it was almost a joke.

寻找我们的幸福 2024-10-26 17:24:24

我使用 HiddenField 的解决方法来决定是否应在回发时重新加载下拉列表。单击按钮后,我设置隐藏字段值。

编辑:
我发现规避该错误的另一种方法是将 DropDown Databind 包含在 try 块内并有一个空的 catch。

try
{
    ddl.DataSource = list;
    ddl.DataTextField = "Text";
    ddl.DataValueField = "Value";
    ddl.DataBind();
}
catch{}

就我的代码而言,我还没有看到任何副作用。

注意:始终使用空的 catch 块并不是一个好主意,因为它会吞掉错误。

I used the workaround of HiddenField to decide if the dropdown should be reloaded upon postback or not. Upon button click I set hidden field value.

Edited:
Another way I found to circumvent the error was to enclose my DropDown Databind inside a try block and have an empty catch.

try
{
    ddl.DataSource = list;
    ddl.DataTextField = "Text";
    ddl.DataValueField = "Value";
    ddl.DataBind();
}
catch{}

I haven't seen any side-effect of this as far as my code is concerned.

Note: It isn't a good idea all the time to have an empty catch block which will swallow the errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文