C# DropdownList.selectedIndex 始终为 0(是的,我确实有!isPostBack)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/312735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:43:58  来源:igfitidea点击:

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

c#asp.netdrop-down-menupostback

提问by Israr Khan

(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.

有一个包含 Datalist 的 asp.net 页面。在这个数据列表中,有一个包含下拉列表的模板,每次数据列表填充一个项目时,都会调用一个 ItemCreatedCommand。itemCreatedCommand 负责对下拉列表进行数据绑定。

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...

我认为问题出在这里,我使用 ItemCreatedCommand 来填充它 - 但奇怪的是,如果我选择颜色“绿色”,页面将自动回发,我会看到下拉菜单仍然是绿色,但是当尝试使用它的 SelectedIndex 时,我总是得到 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.

我已经缩小了一些代码以供查看,但您仍然可以看到我正在尝试做什么:) 我这样做的原因,而不是直接在 aspx-page 中声明颜色的数据源,是我需要运行一个测试 if(showColors),但我不想用我认为应该在代码隐藏文件中的代码弄乱 html 页面。

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

编辑:在尝试更改 SelectedIndexChange 之后 - 我现在脑子里有一个“逻辑”混乱 - 我如何更改数据列表中的元素?因为,据我所知 - 我没有任何方法可以检查这个特定下拉列表属于数据列表中的哪些项目......或者?我将尝试几种方法,看看我最终会得到什么;) 但是请发表您对这个问题的想法:)

SOLUTION:

解决方案:

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

要么将事件冒泡到 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; 
        }

采纳答案by devio

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.

当DataList 是数据绑定时,AutoPostBack 还没有被处理,即ItemCreated 事件中的值仍然是原始值。

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

您需要处理下拉控件的 SelectedIndexChange 事件。

回答by devio

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.

我建议您从下拉列表中删除 AutoPostBack,添加一个“更新”按钮,并更新按钮 Click 事件中的数据。

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

该按钮可以保存 Command 和 CommandArgument 值,因此很容易与数据库记录相关联。

回答by user4132142

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

     }
 }