Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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