Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-24
5
Medium Priority
?
1,102 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 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