Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropdownList.selectedIndex always 0 (yes, I do have !isPostBack)

(Scroll down to bottom of post to find solution.)

Got a asp.net page which contains a Datalist. Inside this datalist, there is a template containing a dropdownlist and each time the datalist is filled with an item, a ItemCreatedCommand is called. The itemCreatedCommand is responsible for databinding the dropdownlist.

I think the problem lies here, that I'm using ItemCreatedCommand to populate it - but the strange things is that if I choose the color "green", the page will autopostback, and I will see that the dropdown is still on the color green, but when trying 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've narrowed down the code a bit for viewing, but still you can see what I'm trying to do :) The reason for why I'm doing this, and not declaring the datasource for the colors directly i aspx-page, is that I need to run a test if(showColors), but I do not want to clutter up the html-page with code that I feel should be in the code behind-file.

EDIT: After trying to alter SelectedIndexChange - I'm having a "logical" confusion in my head now - how am I to alter elements inside the datalist? Since, as far as I know - I do not have any way to check which of the items in the datalist this particular dropdownlist belongs to... Or? I'm going to try out a few ways and see what I end up with ;) But do please post your thoughts on this question :)

SOLUTION:

Either bubble the event to ItemCommand, or Handle the event, get the senders parent(which is a datalistItem and manipulate elements in there.

 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; 
        }
like image 262
Israr Khan Avatar asked Nov 23 '08 17:11

Israr Khan


1 Answers

When the DataList is data-bound, the AutoPostBack has not been handled yet, i.e. the values in the ItemCreated event are still the original values.

You need to handle the SelectedIndexChange event of the dropdown control.

like image 192
devio Avatar answered Sep 29 '22 03:09

devio