OnSelectedIndexChanged event not firing on a DropDownList within a DataList


P

Paul L

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>
 
Ad

Advertisements

S

samuelrobertson

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.
 

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

Top