Click here to Skip to main content
Licence 
First Posted 24 Aug 2007
Views 26,502
Bookmarked 17 times

Error Free DropDownList for Cascading DropDowns

By | 24 Aug 2007 | Article
Only for Solution of databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control

Introduction

If the solution is posted by some Author in CodeProject and if this is duplicate, I am sorry, but I found the work around to above problem and I am just displaying here for your help.

Background

We always need cascading DropDowns, like Selection of Country DropDown and then Selection of State DropDown (Dependent on selected country). And in FormView Templates or any UserControls, cascading DropDowns fail with an error "databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control".

Now DropDownList's DataBind method calls an Event defined by CodeGenerator of ASPX page, which calls Page.GetDataItem() and which raises an exception if it is not called by a Data Binding Container.

Eventually ASPX's DataBinding is designed in such a way that it can only be called by DataBinding Container (FormView etc), if you call FormView's DataBind that will not raise exception.

However, forget about the history, and lets get to solution.

Using the code

Instead of using asp:DropDownList, we suggest use a Web Custom Control "DataBoundDropDownList" derived from DropDownList and has few additional code in it. Which is below...

    public class DataBoundDropDownList : DropDownList
    {
        #region Method protected override void  OnDataBinding(EventArgs e)
        protected override void OnDataBinding(EventArgs e)
        {
            try
            {
                base.OnDataBinding(e);
            }
            catch (InvalidOperationException)
            {
                this.GetData().Select(
                    DataSourceSelectArguments.Empty, 
                    new DataSourceViewSelectCallback(DoSelect));
            }
        }
        #endregion
        private void DoSelect(IEnumerable data)
        {
            this.PerformDataBinding(data);
        }
    }

Thats it guys, Enjoy and forget about this annonying error, which has filled all discussion forums with this problem without any solution !!!!!! and Bug reported at Microsoft always cleared out by saying "Resolved by Design" this means, well there is no solution, just forget it !!! and change your code, change your clients, change your way of living, but we will not give solution !!!!!

When an Exception is raised of type InvalidOperationException, it catches and ReBinds the DropDownList as defined in traditional ListControl class.

I did use Reverse Engineering but only for solving this code.

Warning !!!!

After databind, your old selection will get lost, after calling DataBind you must set Selection Manually, because your new Data Items may not have the old selection which may raise another exception so I decided selection to be left on your choice.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Akash Kava

Web Developer

United States United States

Member

Programmer with WILL

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSelected value does not exist in the list Pinmemberboster_s6:44 27 Dec '10  
Generalsource code PinmemberBrad Oyler8:55 1 May '09  
GeneralTHANK YOU!!! PinmemberMember 23828685:31 13 Mar '09  
GeneralAdds the items twice PinmemberBDheepa4:55 14 Nov '07  
GeneralRe: Adds the items twice PinmemberAkash Kava11:03 14 Nov '07  
GeneralRe: Adds the items twice PinmemberBDheepa2:45 15 Nov '07  
Questionfurther explanation please? Pinmemberrixion10:37 9 Oct '07  
AnswerRe: further explanation please? PinmemberAkash Kava5:49 13 Oct '07  
GeneralRe: further explanation please? PinmemberJalle11:09 19 Apr '12  
GeneralFinally an elegant solution PinmemberCoolfinPartnership4:13 29 Aug '07  
GeneralRe: Finally an elegant solution PinmemberAkash Kava6:58 29 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 24 Aug 2007
Article Copyright 2007 by Akash Kava
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid