Solved

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

Posted on 2008-06-24
5
1,094 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now