Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

972 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