DropDownList does not keep it's SelectedIndex after DataBind

Does anyone know how to solve this puzzle?  I am trying to simply have a dropdownlist in a headertemplate.
The dropdownlist does not keep the selectedindex after making a selection.
Here is my code...Thanks for your help...
LPP

<%@ Page Language="c#" runat="server" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<script language="c#" runat="server">
      public static string Where, OrderBy;
      Hashtable hTable;
      Hashtable hTable2;

      void Page_Load() {
            hTable = new Hashtable();
            hTable2 = new Hashtable();
            
            if (!IsPostBack) {
                  Where = "0";
                  OrderBy = "FirstName";
                  LoadDataList();
                  Response.Write(Where);
                  Response.Write("<br>" + OrderBy);
            } else {
            }
      }
      
      void LoadDataList() {
            hTable2.Add("EventID", "6");
            dl1.DataSource = hTable2;
            dl1.Visible = true;
            dl1.DataBind();
      }
      
      void SelectIndex(object sender, System.EventArgs e) {
            DropDownList list = (DropDownList)sender;
            Where = list.SelectedItem.Value;
            LoadDataList();
            Response.Write(Where);
            Response.Write("<br>" + OrderBy);
      }
      
      void LinkButtonCommand(object sender, DataListCommandEventArgs e) {
            OrderBy = e.CommandName;
    foreach(DataListItem item in dl1.Items) {
      DropDownList ddl = item.FindControl("dropdownlist") as DropDownList;
      if (ddl == null) continue;
      ddl.SelectedIndex = Convert.ToInt32(Where);
    }
            LoadDataList();

            Response.Write(Where);
            Response.Write("<br>" + OrderBy);
      }
</script>
<html>
<body>
<form id="myform" name="myform" runat="server">
<asp:DataList ID="dl1" ShowHeader="true" runat="server" OnItemCommand="LinkButtonCommand" ExtractTemplateRows="false" Visible="false">
      <headertemplate>
            <asp:DropDownList ID="dropdownlist" AutoPostBack="true" OnSelectedIndexChanged="SelectIndex" runat="server">
                  <asp:ListItem Text="ALL" Value="0"></asp:ListItem>
                  <asp:ListItem Text="1" Value="1"></asp:ListItem>
                  <asp:ListItem Text="2" Value="2"></asp:ListItem>
                  <asp:ListItem Text="3" Value="3"></asp:ListItem>
            </asp:DropDownList>
            <br>
            <asp:LinkButton ID="linkbutton" CommandName="FirstName" runat="server">FirstName</asp:LinkButton>
            <asp:LinkButton ID="linkbutton2" CommandName="LastName" runat="server">LastName</asp:LinkButton>
      </headertemplate>
      <itemtemplate>
            <asp:ImageButton ID="edittemplate" runat="server" ImageUrl="images/view.jpg" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "Value")%>' />
      </itemtemplate>
</asp:DataList>
</form>
</body>
</html>
lpparkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

martie_11Commented:
Is ViewState set to true for the drop down?
lpparkerAuthor Commented:
I added  EnableViewState="true" to the dropdownlist and it still keeps defaulting to the first in the list.
amit_gCommented:
You are calling LoadDataList in OnSelectedIndexChanged. That would reset any selection. Is there any reason for it? If not, comment it...

     void SelectIndex(object sender, System.EventArgs e) {
          DropDownList list = (DropDownList)sender;
          Where = list.SelectedItem.Value;
          //LoadDataList();
          Response.Write(Where);
          Response.Write("<br>" + OrderBy);
     }
     
lpparkerAuthor Commented:
In the actual build code, I am trying select a WHERE clause and repopulate the DataList by rebuilding the SQL after changingindex on the dropdownlist.
So, as I understand it, I need to recall the databind... am I right?
Appreciate any feedback.

This was one solution I had, but don't really like it......

<asp:DropDownList
     ID="dropdownlist"
     EnableViewState="true"
     AutoPostBack="true"
     OnSelectedIndexChanged="SelectIndex"
     runat="server"
     SelectedIndex='<%#Convert.ToInt32(Where)%>'>     //<<---This line was added... not preferred.

LPP
Anandhi KAssociate DirectorCommented:
Add ItemDataBound Event Handler in .cs file.
this.dl1.ItemDataBound += new DataListItemEventHandler(dl1_ItemDataBound);
In the event Handler, add the following code and it will work.

private void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
      if(e.Item.ItemType == ListItemType.Header)
     {
        DropDownList list = e.Item.FindControl("dropdownlist") as DropDownList;
        if(list != null)
        {
      list.ClearSelection();
      foreach(ListItem item in list.Items)
      {
          if(item.Value == Where)
         {
             item.Selected = true;
             break;
         }
      }
      }
  }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.