Solved

How do I programmatically set the selectedValue of cascading dropdownlists in C#

Posted on 2008-06-24
5
1,100 Views
Last Modified: 2013-12-16
I have an ASP.NET 2.0 app that has multiple pages with 2 dropdownlists on.
ddl1 = CostCentre
ddl2 = Employee

Both ddl are bound to sqldatasources and ddl2 has a parameter which is bound to the value of ddl1. This all works fine.

What i want to do is when the user moves from one page to another the dropdownlists are set to the Costcentre and Employee that was selected on the previous page.

The costcentre ddl i have no problem with but its setting the value of the Employee ddl which relies on the costcentre being selected.

Session variable are set with the selected values of the ddl's.

i have tried this code but the employee ddl ends up empty with no value selected.

protected void Page_Load(object sender, EventArgs e)
    {
            if (Session["CC"] != null)
            {
                ddCCList.SelectedValue = (string)Session["CC"];                
                ddEmployeeList.DataBind();
                if (Session["EmpID"] != null)
                {
                    ddEmployeeList.SelectedValue = (string)Session["EmpID"];
                }
            }        
    }

i have also tried putting similar code in the ondatabound event of the dropdownlists but that doesn't work either.

Any help appreciated.

Anthony
0
Comment
Question by:christiegroup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:ydramu
ID: 21855937
Could you verify once your Session["CC"] value is not null.
0
 
LVL 7

Expert Comment

by:ydramu
ID: 21855968
If it has value then try with this way.

ddCCList.Items.FindByText((string)Session["CC"]).Selected = true;
0
 
LVL 1

Author Comment

by:christiegroup
ID: 21856550
Hi vdramu,

i get "Object reference not set to an instance of an object. " if i use

ddCCList.Items.FindByText((string)Session["CC"]).Selected = true;

0
 
LVL 7

Expert Comment

by:ydramu
ID: 21856646
Please try with this way.

Seems your Session value getting null.    

And try do the same null varification for the EmpID and then try that variable with Drop down list.
  if (Session["CC"] != null)
            {
 
                string selectValue= (string)Session["CC"];             
                ddCCList.Items.FindByText(selectValue).Selected = true;
                ............
                ............
             }

Open in new window

0
 
LVL 1

Accepted Solution

by:
christiegroup earned 0 total points
ID: 21857409
Hi vdramu,

I still get the "Object reference not set to an instance of an object." but i have found a way to do it.

Putting the code in the databound event of the dropdownlist works fine. I tried this before but i had ddEmployeeList.DataBind(); in the ddCCList_DataBound event which causes problems. I have tried it again taking it out and it now works

Here is the code.

protected void ddCCList_DataBound(object sender, EventArgs e)
    {
        if (Session["CC"] != null)
        {
            ddCCList.SelectedValue = (string)Session["CC"];            
        }
    }
    protected void ddEmployeeList_DataBound(object sender, EventArgs e)
    {
        if (Session["EmpID"] != null)
        {
            ddEmployeeList.SelectedValue = (string)Session["EmpID"];
        }
    }  

Thanks for trying to help with this.

Anthony
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question