OnSelectedIndexChanged event not firing on a DropDownList within a DataList

Discussion in 'ASP .Net' started by Paul L, May 6, 2005.

  1. Paul L

    Paul L Guest

    Hi, I have an issue with the OnSelectedIndexChanged event not firing for a
    DropDownList control which is in the ItemTemplate of a DataList.

    I have made an exact copy of the DropDownList control, and placed it outside
    of the DataList and it fires the event just fine. So it's definitely to do
    with it being in a template.

    I've managed to reproduce the issue on a small test app, the code and html
    is below:

    Thanks in advance,

    Paul


    *** Code behind ***

    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label output;
    protected System.Web.UI.WebControls.DropDownList Dropdownlist1;
    protected System.Web.UI.WebControls.DataList DataList1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here

    ArrayList list = new ArrayList();
    list.Add("item1");
    list.Add("item2");
    list.Add("item3");
    DataList1.DataSource = list;
    DataList1.DataBind();
    }

    public void SelectionChanged(object sender, EventArgs e)
    {
    output.Text = "Changed";
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

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

    }
    #endregion
    }


    *** HTML / ASP ***

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
    Inherits="TestBed.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <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="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:DropDownList ID="Dropdownlist1" Runat="server"
    EnableViewState="True" AutoPostBack="True"
    OnSelectedIndexChanged="SelectionChanged">
    <asp:ListItem Selected="True" Value="1">One</asp:ListItem>
    <asp:ListItem Selected="False" Value="2">Two</asp:ListItem>
    <asp:ListItem Selected="False" Value="3">Three</asp:ListItem>
    </asp:DropDownList>
    <asp:DataList id="DataList1" style="Z-INDEX: 101; LEFT: 232px; POSITION:
    absolute; TOP: 72px"
    runat="server">
    <ItemTemplate>
    <asp:DropDownList ID="dropDown" Runat="server" EnableViewState="True"
    AutoPostBack="True" OnSelectedIndexChanged="SelectionChanged">
    <asp:ListItem Selected="True" Value="1">One</asp:ListItem>
    <asp:ListItem Selected="False" Value="2">Two</asp:ListItem>
    <asp:ListItem Selected="False" Value="3">Three</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:DataList>
    <asp:Label id="output" style="Z-INDEX: 102; LEFT: 472px; POSITION:
    absolute; TOP: 88px" runat="server">Label</asp:Label>
    </form>
    </body>
    </HTML>
     
    Paul L, May 6, 2005
    #1
    1. Advertising

  2. Paul L

    Guest

    My guess is the ID of the dropdownlist is changing between postbacks.

    Can you put a breakpoint at the end of your page load and drill down
    into the datalist and check the id of the dropdownlist and check where
    it is in the control collection. It must be in the same place in the
    control tree for restore view state to work, and as you already know
    view state has to be enabled for this change event to fire. Also check
    enableviewstate on the dropdownlist and every one of its .Parent(s) all
    the way to the top. If even one of these is set to false the event
    won't be fired.

    You should check all this on page postback, not the initial load.
     
    , May 6, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Reichman

    Dropdownlist onselectedIndexChanged event not fires

    Alexander Reichman, Oct 17, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    23,329
  2. =?Utf-8?B?UmVkR1NU?=
    Replies:
    1
    Views:
    4,397
    Karl Seguin
    Mar 15, 2005
  3. Jerry

    DropDownList - onSelectedIndexChanged not firing

    Jerry, Apr 15, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    203
    Jerry Higgins
    Apr 16, 2004
  4. Paul L
    Replies:
    0
    Views:
    256
    Paul L
    May 5, 2005
  5. Alexander Reichman

    Dropdownlist onselectedIndexChanged event not fires

    Alexander Reichman, Oct 17, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    201
Loading...

Share This Page