troubleshooting Question

<asp:DropDownList>, the OnSelectedIndexChanged event, and Databinding

Avatar of Member_2_3718378
Member_2_3718378 asked on
6 Comments1 Solution1036 ViewsLast Modified:
I'm having problems when trying to use the <asp:DropDownList> control's OnSelectedIndexChanged event to retrieve a databound value.  I'm using C# / ASP.NET (.NET v2.0) on WinXP SP2... here's my code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title><asp:DropDownList> and the OnSelectedIndexChanged event</title>
    <form id="form1" runat="server">
            <asp:DropDownList ID="MyList" runat="server" style="width:296px;" AutoPostBack="true" OnSelectedIndexChanged="MyList_SelectedIndexChanged">
                  <asp:ListItem Text="SELECT" Value="" />
                  <asp:ListItem Text="------------------------------------------" Value="" />
                  <asp:ListItem Text="A" Value="1" />
                  <asp:ListItem Text="B" Value="2" />

#region Declare namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Globalization;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page {
      #region Completely disable ViewState.
      override protected void SavePageStateToPersistenceMedium(object viewState) { }
      override protected object LoadPageStateFromPersistenceMedium() {
            object returnValue = null;
            return (returnValue);
      override protected object SaveViewState() {
            object returnValue = null;
            return (returnValue);

      protected void MyList_SelectedIndexChanged(object sender, EventArgs e) {
            Response.Write("MyList_Changed fired.<br>");
            Response.Write("MyList.Value = [" + MyList.SelectedIndex.ToString() + "]<br>");
      protected void Page_Load(object sender, EventArgs e) {
            bool criticalErrorHasOccurred = false;
            string errorCode = String.Empty, sqlQuery = String.Empty;
            using (SqlConnection myConnection = new SqlConnection()) {
                  #region Open database connection
                  try {
                        myConnection.ConnectionString = WebConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
                  } catch {
                        criticalErrorHasOccurred = true;
                        errorCode = "DatabaseConnection";

                  if (!criticalErrorHasOccurred) {
                        sqlQuery = "EXEC SomeStoredProcedure";
                        SqlCommand sqlCommand = new SqlCommand(sqlQuery, myConnection);
                        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

                        if (sqlDataReader.HasRows) {
                              MyList.Visible = true;
                              MyList.AppendDataBoundItems = true;
                              MyList.DataTextField = "sTitle";
                              MyList.DataSource = sqlDataReader;


When I select one of the static values in the dropdown list, that I declare in my ASPX page (i.e. "A" or "B"), the page works fine.  But when I select a databound value, that I retrieve from SQL Server in my CS page, a SelectedIndex value of zero ("0") is always printed out by my SelectedIndexChanged() method.  Does anybody know what I'm doing that's preventing me from retrieving databound values via my event handler?

-= DeathToSpam =-
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros