0

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.

gbs
  • 7,196
  • 5
  • 43
  • 69
  • The sample code is not accessible for the download. Permission problem? – Oleg Mar 02 '11 at 08:52
  • Sorry about that. I just clicked the link and was able to download without logging in. I'm using skydrive first time so not sure what could be wrong. I alreay have set it up as public. Can you try by logging in to windows-live if you have an account? – gbs Mar 02 '11 at 15:39
  • @gbs: I have to login in windows-live but after the login I receive the message in German which could be translated as "This folder is for you may not be released (shared). They seem to have no rights to access to Cascading. You might try using a different account on it, or you have to ask the owner of the folder to the authorization for access." The same problem I has from different browsers. From your account you have access, but you should permit other people to access the file. – Oleg Mar 02 '11 at 16:53
  • Please use @Oleg in the comment to notify me about the comment. See [here](http://meta.stackexchange.com/questions/43019/how-do-comment-replies-work) for more information. – Oleg Mar 02 '11 at 16:56
  • @Oleg : Please try now. I have change access rights. Hope that works. – gbs Mar 02 '11 at 21:22
  • @gbs: I can download now, but I am a little disappoint, that you use really retro technique which about 10 yeas old. Which version of .NET you use? Why you can not use WCF or at least ASMX web services instead of ASHX. Moreover you make manual JSON serialization instead of the usage at least `JavaScriptSerializer` existing in .NET 3.5. Manual serialization will work wrong if the texts contain " or \ which must be encoded as \" and \\. My first problem is that I can not debug it because I don't have version `1.0.61025.0` of `System.Web.Extensions`. So I have to convert it in .NET 4.0 – Oleg Mar 02 '11 at 21:34
  • @gbs: I was writing another answer at the moment. It would be better if you describe me the reason of the usage of retro technology and answer what the Operation System and .NET framework which are on the destination servers? – Oleg Mar 02 '11 at 21:36
  • @gbs:I must agree with Oleg. You're trying to populate the dropdowns client-side, but those are server controls. I guess there's a problem with the viewstate. The controls are trying to rebind but something has changed in the meanwhile.I don't have a solution for you cause your code is too complex ... for such a simple problem. You should do something like this: http://weblogs.asp.net/jaredroberts/archive/2009/08/28/great-article-on-cascading-dropdown-list-and-jquery.aspx – LeftyX Mar 02 '11 at 22:01
  • @Oleg: .NET 2.0. I use custom httphandler which is inside of a class library. For creating a sample I used .ashx. From my understanding using .asmx or WCF will also result into the error I am seeing. I should have removed unwanted references like System.Web.Extensions and all. Sorry about that and thanks for looking into the code. – gbs Mar 02 '11 at 22:02
  • @Oleg and @gbs: ashx with asp.net webforms is fine.I've used it in the past and it does the job.The whole implementation is just overly complex. – LeftyX Mar 02 '11 at 22:06
  • @LeftyX: Yeah I have that article already open and I reached there from it's base article on mikesdotnetting. Anyway the point is yes, I have more complex system than what I have in sample code. There are other external variables involved which depends on year/make/model selection. I use hiddenfields as mentioned in the article and repopulate my dropdowns on postback for any changes on client-side i.e. ReloadDropDownSelection in code-behind. That actually is the culprit for the error. If I comment it out I won't see the error when I click Clear but then I lose the values when I click Submit. – gbs Mar 02 '11 at 22:24
  • @LeftyX: The thing is that there are cases where I have to populate dropdown from code-behind and there are other controls that take the dropdown selection as input to populate themselves with filtered data. If this wasn't needed then the code in my .ashx was sufficient and I wouldn't need anything in my .ascx code-behind and it would be very simple as you said. Thanks for looking into it. – gbs Mar 02 '11 at 22:36
  • @gbs and @LeftyX: I agree that all technological has his own area which it is better as other. Sometimes one have to use an old code so one don't want rewrite all. My point of view is: `jQuery.ajax` need get **data** from the server. The best way provide the data are web services in form of WFC (mostly performer and flexible way) and ASMX. One can good mix WFC and ASMX with any other technology. See for example my [other answer](http://stackoverflow.com/questions/4680162/host-wcf-in-mvc2-site/4680617#4680617). One can add just one more page/url which will provide **data** for jQuery.ajax. – Oleg Mar 02 '11 at 22:51
  • @gbs: I finished to answer another question and been back. The first thing which I found in your code: you have no usage of `jQuery.ajax`. You wrote in the first sentence of your question: "I have Cascading Dropdowns, populated via jquery ajax". Do I miss something? Do you uploaded the correct code? – Oleg Mar 02 '11 at 22:54
  • @Oleg: Yes, the code uploaded is correct. I call $.getJSON which I believe is shorthand for jQuery.ajax. It is inside /scripts/cascading.js and the dropdown onchange is hooked-up in CascadingUC.ascx.cs. – gbs Mar 02 '11 at 23:16
  • @gbs: Sorry I found the code very quickly, but not posted comment about it. I tried to understand the code, but it is far from what I usually do. Till now I don't understand the logic of updates `SelectedIndex` for different models in `CascadingUC.ascx.cs` after changing of it's contain per ajax. Moreover I don't see any clear data model separation between `CascadingUC.ascx.cs` and `CascadeHandler.ashx`. Probably you will find interesting pure `jQuery` solution from my [old answer](http://stackoverflow.com/questions/4954081/chained-state-city-dropdown-list-from-json/4955856#4955856) with demo. – Oleg Mar 03 '11 at 00:01
  • @Oleg: In my real code the data comes from the database and is common for handler as well as .ascx.cs. The idea behind Updating SelectedYear/Make/Model is to repopulate the dropdown items upon postback otherwise the dropdown will be out-of-sync if user made selection change as it is purely via ajax. To understand it just try to comment out this line ReloadDropDownSelection(); in .ascx.cs page_load. Then run the page and do a make model selection and press Submit. You will see the dropdown loses the selection. – gbs Mar 03 '11 at 00:44

2 Answers2

0

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.

LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • Thanks for looking into the code but as I mentioned in my question - "I have a weird way to circumvent it by setting hiddenfield upon Clear click". That is similar to your but my workaround is generic. The biggest issue is my page is very complex and there are several other postback controls whose action affects the dropdown UC. Right now for each such control I decide if I should call ReloadDropDownSelection. It is working fine. Also the .ascx control is going to be used on other pages as well and so it will be a headache. – gbs Mar 03 '11 at 15:44
  • Yes, but the thing is, you want your life to be complicated. At the end of the day you're loading a bunch of dropboxes in cascade and you need to read the value and, eventually, clear them. The process in very simple and clear. You code is very complex.I used to do that in the past and used to spend 2 days to understand the event calls etc etc.Funny thing is the web is stateless:you read data and post data. – LeftyX Mar 03 '11 at 19:23
0

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.

gbs
  • 7,196
  • 5
  • 43
  • 69