SelectedIndex on DropDownList doesn't fire

Posted on 2009-04-21
Last Modified: 2012-06-22
I have a usercontrol which have two dropdown controls. I wan't to make the bottom one dependant on the top one, so when the user selects an item in the top list, the bottom gets updated with new values.

The top dropdownlist has a list of subdepartments, and the bottom a list of brands. When the page is loaded, both dropdowns have the full list, but after the subdepartment is selected, the brands are filtered to only show the brands in that subdepartment.

Should be a breeze, but I can't get the SelectedIndexChanged event to fire. I'm trying to use controlstate to save values between postbacks. The hole thing is complicated by the fact that the Page in which the control resides has EnableViewState set to false, and this cannot be changed (too long to explain).

Can somone help?

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BrandsListBox.ascx.cs"

    Inherits="Modules_BrandsListBox" EnableViewState="true" %>   

<div style="width: 239px; height: 180px; background: #FFFFFF url(../Images/illu/department_10.jpg) no-repeat;">

    <asp:UpdatePanel ID="m_updPanelBrandsBanner" runat="server" EnableViewState="true">


        <div style="padding: 10px;">

            <img id="img1" src='<%# string.Format( "~/images/ImageText.aspx?text={0}&imagetypeId=greywhite24", Server.UrlEncode( Resources.Airshop.BrandHeader ) ) %>'

                alt='' runat="server" enableviewstate="true" style="margin-left: 72px;" />

            <div style="padding: 5px; text-align: right;">


                <asp:DropDownList ID="ddlSubdepartments" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlSubdepartments_SelectedIndexChanged" />

                <br />                

                <br />

                <asp:Label ID="lblBrands" runat="server" Text="Vælg mærke: " EnableViewState="false" />


                <asp:DropDownList ID="ddlBrands" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlBrands_SelectedIndexChanged" />

                &nbsp;<br />                

                <br />               


                <asp:HyperLink ID="lnkAllBrands" runat="server" EnableViewState="true" Text="Se alle vore mærker >>"

                    NavigateUrl="~/Brands.aspx" />






using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class Modules_BrandsListBox : System.Web.UI.UserControl


    AirshopBLC airShop = new AirshopBLC();


    protected void Page_Load(object sender, EventArgs e)




        if (!IsPostBack)




                m_BrandDataSource = airShop.GetBrands();




                // Error handling







    // Doesn't fire. Why? Because index isn't changed.

    protected void ddlSubdepartments_SelectedIndexChanged(object sender, EventArgs e)





    // Doesn't fire. Why?

    protected void ddlBrands_SelectedIndexChanged(object sender, EventArgs e)


        Response.Redirect(string.Format("~/Brands.aspx?BrandId={0}", ddlBrands.SelectedItem.Value));


     //Need to store the DataSource in ControlState because

     //ViewState probably is disabled

    protected override void OnInit(EventArgs e)





    protected override object SaveControlState()


        object[] o = new object[] { 



        return (object)o;


    protected override void LoadControlState(object state)


        //m_BrandDataSource = (BrandDepartmentsDS)state;

        if (state != null)


            object[] lastRequest = state as object[];

            m_BrandDataSource = lastRequest[0] as BrandDepartmentsDS;

            //ddlSubdepartments.SelectedIndex = (int)lastRequest[1];

            //ddlBrands.SelectedValue = (string)lastRequest[2];



    private void BindSubDepartments()


        DataView dv = new DataView();

        dv = BrandDataSource.SubDepartments.DefaultView;

        ddlSubdepartments.DataTextField = "SubDeptName";

        ddlSubdepartments.DataValueField = "SubDeptId";

        ddlSubdepartments.DataSource = dv;


        ddlSubdepartments.Items.Insert(0, new ListItem("-- Vælg afdeling --", "-- Vælg afdeling --"));


    private void BindBrands(string subDeptId)


        DataView dv = new DataView();

        if (!string.IsNullOrEmpty(subDeptId))


            dv = BrandDataSource.BrandSubDepartments.DefaultView;

            dv.RowFilter = "SubDeptId = " + subDeptId;



            dv = BrandDataSource.Brands.DefaultView;

        ddlBrands.DataTextField = "BrandName";

        ddlBrands.DataValueField = "BrandId";

        ddlBrands.DataSource = dv;


        ddlBrands.Items.Insert(0, new ListItem("-- Vælg mærke --", "-- Vælg mærke --"));


    private BrandDepartmentsDS m_BrandDataSource;

    public BrandDepartmentsDS BrandDataSource


        get{ return m_BrandDataSource; }

        set { m_BrandDataSource = value; }



Open in new window

Question by:MYTAIR
    LVL 14

    Accepted Solution


    SelectedIndexChanged is based on the ViewState. If ViewState=false, then the values won't be there in the ViewState.
    LVL 4

    Expert Comment

    You should set the AutoPostBack of your dropdow to 'true'. Then the event will fire.


    Author Comment

    Hi GiftsonDJohn,
    I can see that you're right. It's not possible to use SelectedIndex without ViewState.

    Is there another way to maintain state of a dropdownlistbox? I've tried using ControlState, but nothing gets saved there either.

    Author Comment

    I discovered the way forward in this brilliant article:

    You can simply add your own custom DropDownList and get SelectedIndex to fire, and also save SelectedValue across Postbacks.

    Thanks to Giftson for spotting the problem.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 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

    22 Experts available now in Live!

    Get 1:1 Help Now