Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

c# - 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; 
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...