简体   繁体   中英

SelectedIndexChanged event not firing inside the footer of a Datalist

I´ve been searching the web and Stackoverflow for this answer, I´ve tried some but with no success. My problem: I have a dropdownlist inside the footer of a Datalist. My page´s AutoEventWireup is set to true. My Dropdown´s autopostback is set to true. I bind the event on itemcreation of the datalist. The dropdown does get the postback but doesn´t call the function set at the SelectedIndexChanged event.

On create:

protected void dlCartItemsMonetary_ItemCreated(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Footer)
    {
        DropDownList combo = (DropDownList)e.Item.FindControl("ddlDeliveryService");
        if (combo != null)
        {
            combo.SelectedIndexChanged += new EventHandler(ddlDeliveryService_SelectedIndexChanged);
        }
    }
}

The dropdown:

<asp:DropDownList ID="ddlDeliveryService" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlDeliveryService_SelectedIndexChanged" EnableViewState="true">

It does the postback but it doesn´t enter the ddlDeliveryService_SelectedIndexChanged function.

Can anyone give me some tips on how to solve it?

Thanks in advance

Resolved!

My problem and many other's is that I was setting the DataSource and DataBinding the DataList on every postback. As soon as I added the postback validation the event begin to fire. Code below:

    if (this.Page.IsPostBack == false)
    {
        dlCartItemsMonetary.DataSource = list;
        dlCartItemsMonetary.DataBind();
    }

If you are having the same problem with events not firing check your code for the same issue!

Thanks jpartjh for your comment, it made me think about the lifecycle of the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM