DropdownList.selectedIndex is always 0 (yes, I have! IsPostBack)

(Scroll down to the end of the message to find a solution.)

Get the asp.net page that contains the DataList. Inside this datalist there is a template containing a drop-down list and each time the datalist is filled with an element, a ItemCreatedCommand is called. itemCreatedCommand is responsible for the databinding of the drop-down list.

I think the problem here is that I use ItemCreatedCommand to populate it - but the weird thing is that if I select the color "green", the page will be auto-retransmitted, and I will see that the drop-down list is still green, but when I try to use it's SelectedIndex, I always get 0 ...

protected void DataListProducts_ItemCreatedCommand(object
    source, DataListItemEventArgs e)

 var itemId = (String)DataListProducts.DataKeys[e.Item.ItemIndex];
 var item = itemBLL.GetFullItem(itemId); 

 var DropDownListColor = (DropDownList)e.Item.FindControl("DropDownListColor");

 //Also tried with :
 //if(!isPostBack) {

 DropDownListColor.DataSource = item.ColorList;
 DropDownList.Color.Databind();

 // } End !isPostBack)

    Label1.test = DropDownListColor.SelectedIndex.toString();
 // <- THIS IS ALWAYS 0! *grr* 

, , :) , i aspx-page, , if (showColors), html- , .

EDIT: SelectedIndexChange - "" - DataList? , , , datalist this ... ? , ;) , :)

:

ItemCommand, , ( datalistItem .

 protected void DropDownListColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dropDownListColor = (DropDownList)sender;
            DataListItem dataListItem = (DataListItem)dropDownListColor.Parent;

            var item = items[dataListItem.ItemIndex];
            var color = item.ItemColor[dropDownListColor.SelectedIndex];

            var LabelPrice = (Label)dataListItem.FindControl("LabelPrice");
            LabelPrice.Text = color.Price; 
        }
+5
4

DataList , AutoPostBack , .. ItemCreated - .

SelectedIndexChange .

+8

:

AutoPostBack , "" Click event.

Command CommandArgument, .

0

 protected void ddlOnSelectedIndexChanged(object sender, EventArgs e) {
     try {
         ModalPopupExtender1.Show();
         if (ViewState["Colors"] != null) {
             FillColors(ViewState["Colors"].ToString());
         }

         DropDownList dropDownListColor = (DropDownList)sender;
         DataListItem dataListItem = (DataListItem)dropDownListColor.Parent;

         Image image = (Image)dataListItem.FindControl("mdlImage");
         Label ProductCode = (Label)dataListItem.FindControl("lblprdCode");
         Label ProductName = (Label)dataListItem.FindControl("lblProdName");
         DropDownList ddlQuantity = (DropDownList)dataListItem.FindControl("ddlQuantity");
         Label ProductPrice = (Label)dataListItem.FindControl("lblProdPrice");
         Label TotalPrice = (Label)dataListItem.FindControl("lblTotPrice");
         //Label ProductPrice = (Label)dataListItem.FindControl("lblProdPrice");
     } catch (Exception ex) {

     }
 }
0

All Articles