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; 
        }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

Regarding your 2nd question:

I suggest you remove the AutoPostBack from the dropdown, add an “Update” button, and update the data in the button Click event.

The button can hold Command and CommandArgument values, so it’s easy to associate with a database record.

Method 3

some MSDN links with C# examples on bubbling

http://msdn.microsoft.com/en-us/library/system.web.ui.control.onbubbleevent.aspx

http://msdn.microsoft.com/en-us/library/aa719644(VS.71).aspx

http://msdn.microsoft.com/en-us/library/aa720044(VS.71).aspx

Method 4

Thank You for your solution

 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) {

     }
 }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x