Link to home
Start Free TrialLog in
Avatar of MYTAIR
MYTAIR

asked on

SelectedIndex on DropDownList doesn't fire

Hi,
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?
Cheers
Jens


<%@ 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">
        <ContentTemplate>
        <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" />
            </div>
        </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
 
 
 
 
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)
    {
        img1.DataBind();
        
        if (!IsPostBack)
        {
            try
            {
                m_BrandDataSource = airShop.GetBrands();
            }
            catch
            {
                // Error handling
            }           
        }
       
        BindBrands("");
        BindSubDepartments();
    }
 
    // Doesn't fire. Why? Because index isn't changed.
    protected void ddlSubdepartments_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSubDepartments();
        BindBrands(ddlSubdepartments.SelectedItem.Value);
    }
 
    // 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)
    {
        Page.RegisterRequiresControlState(this);
        base.OnInit(e);
    }
 
    protected override object SaveControlState()
    {
        object[] o = new object[] { 
            (object)m_BrandDataSource
        };
        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.DataBind();
        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;
        }
        else
            dv = BrandDataSource.Brands.DefaultView;
        ddlBrands.DataTextField = "BrandName";
        ddlBrands.DataValueField = "BrandId";
        ddlBrands.DataSource = dv;
        ddlBrands.DataBind();
        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

ASKER CERTIFIED SOLUTION
Avatar of GiftsonDJohn
GiftsonDJohn
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of amazingwolf1
amazingwolf1

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

HTH
Avatar of MYTAIR

ASKER

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.
Avatar of MYTAIR

ASKER

I discovered the way forward in this brilliant article:
http://www.harvardinnovations.com/LearningZone/WhatisASP_NET/DropdownlistEnableViewStateProblem

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.