troubleshooting Question

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

Avatar of Member_2_3718378
Member_2_3718378 asked on
ASP.NET
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:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title><asp:DropDownList> and the OnSelectedIndexChanged event</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <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" />
            </asp:DropDownList>    
    </div>
    </form>
</body>
</html>


----------------------------------------------------------------------------------------------------
Default.aspx.cs
----------------------------------------------------------------------------------------------------
#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;
#endregion

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);
      }
      #endregion

      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;
                        myConnection.Open();
                  } catch {
                        criticalErrorHasOccurred = true;
                        errorCode = "DatabaseConnection";
                  }
                  #endregion

                  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;
                              MyList.DataBind();
                        }

                        sqlDataReader.Close();
                  }
            }
      }
}
========================================================


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 =-
ASKER CERTIFIED SOLUTION
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