Databinding a drop down list, hooking into the SelectedIndexChanged and avoiding the viewstate


Dave A

I am stuggling with databinding a drop down list, hooking into the SelectedIndexChanged and attempting to avoid using the viewstate.

The drop down list is quite large so I would prefer to avoid using the view state so I set EnableViewState on the page to false. To enable the drop down list to bind to a datasource I bind it during the OnInit.

I do a response.Write during the SelectedIndexChanged event to verify that the event fires.

It all works EXCEPT when the first item of the drop down list is reselected after another item is selected. The event just does not fire.

Does this mean that if you want to hook into SelectedIndexChanged reliably you must use the ViewState?

Dave A

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" enableViewState="False"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="">
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 72px"
runat="server" Width="280px" AutoPostBack="True"></asp:DropDownList>


using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
protected System.Web.UI.WebControls.DropDownList DropDownList1;
NameValueCollection list = new NameValueCollection();

protected WebForm1()
list.Add("Do not filter","");

private void Page_Load(object sender, System.EventArgs e)


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
DropDownList1.DataSource = list;

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);


private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
Response.Write("Drop down list fired:" + DropDownList1.SelectedValue);




You maybe should define DropDownList1.Datamember.

I am also struggling with databinding and dropdownlist: It works in a
aspx page but not in an ascx component, do you know why?



Dave A

If you could provide a cut down example I would love to take a look. This
stuff is all 'too easy' when you read it in the help or in a news group or
in a book but when you are faced with real-world problems the wheels seem to
fall off.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question