views:

900

answers:

5

I am generating a dropdown list in codebehind and cannot get the selectedindexchanged event to fire automatically. It works fine when put directly into the ASPX page, but I need it to be in the codebehind.

This doesn't work -

var deptList = new DropDownList
                               {
                                   ID = "deptList",
                                   DataSource = departments,
                                   DataTextField = "deptname",
                                   DataValueField = "deptid",
                                   AutoPostBack = true,
                                   EnableViewState = true
                               };
            deptList.SelectedIndexChanged += new EventHandler(deptList_SelectedIndexChanged);
            deptList.DataSource = departments;
            deptList.DataTextField = "deptname";
            deptList.DataValueField = "deptid";
            if (!IsPostBack)
                deptList.DataBind();
            deptList.Items.Insert(0, new ListItem("---Select Department---", string.Empty));

            writer.Write("Select a department: ");
            deptList.RenderControl(writer);

but this works - <asp:DropDownList ID="deptList" AutoPostBack="true" runat="server" OnSelectedIndexChanged="deptList_SelectedIndexChanged"></asp:DropDownList>

+6  A: 

The problem may be if you are not adding the control to the page early enough. Controls need to be added early in the page lifecycle to get their events tied in.

You're probably doing it in the Load event, which is too late. Try adding it during the Init event or overriding the CreateChildControls method.

Edit: As Dave Swersky mentioned, make sure you do this on EVERY page request including postbacks.

Mike Mooney
So since I am rendering it to an HTMLTexWriter, would I render it to that in the Init or Load?
Alex
I put the entire block of code into the page_init and nothing happens still
Alex
Compare the markup between the working implementation (markup) and not-working (code) in Firebug or the IE development console. Something's different.
Dave Swersky
+1  A: 

You have a mesh in your code. Try to devide creating, data binding and events calling.

Example:

<asp:DropDownList ID="deptList" AutoPostBack="true" runat="server"></asp:DropDownList>

Then in code behind (Page_Load):

deptList.SelectedIndexChanged += new EventHandler(deptList_SelectedIndexChanged);

if (!IsPostBack)
{
     deptList.DataTextField = "deptname";
     deptList.DataValueField = "deptid";
     deptList.DataSource = departments;
     deptList.DataBind();
     deptList.Items.Insert(0, new ListItem("---Select Department---", string.Empty));
}
sashaeve
+2  A: 

To elaborate on Mike Mooney's answer: you also need to make sure you add the control back into the control tree on every postback. The control tree is recreated on each postback, read in from the markup. If you add it once programmatically and never again, there is no control in the tree to receive the event.

Dave Swersky
Thanks Dave. Yes, I neglected to mention that very important part, the controls need to ALWAYS be created, regardless of whether it is a postback. That's caught me several times before as well.
Mike Mooney
A: 

It appears that you are not adding the control to the controls collection. You must add the control somewhere in the control hierarchy and ensure it gets added on every postback to ensure the control exists to receive the event. By adding the control you shouldn't need to call RenderControl directly.

HectorMac
A: 

tanks for evry body ;)

Naim