DropdownList.selectedIndex 始终为 0(是的,我确实有 !isPostBack) [英] DropdownList.selectedIndex always 0 (yes, I do have !isPostBack)

查看:18
本文介绍了DropdownList.selectedIndex 始终为 0(是的,我确实有 !isPostBack)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(向下滚动到帖子底部以找到解决方案.)

有一个包含一个 asp.net 的页面数据列表.在这个数据列表中,有是一个包含一个的模板下拉列表和每次datalist 填充了一个项目,一个ItemCreatedCommand 被调用.这itemCreatedCommand 负责数据绑定下拉列表.

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.

我认为问题出在这里,那我正在使用 ItemCreatedCommand填充它 - 但奇怪的事情是如果我选择颜色绿色",页面会自动回发,我会看到下拉菜单还在颜色为绿色,但在尝试使用时它是 SelectedIndex,我总是得到 0...

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* 

我已经将代码范围缩小了一点观看,但你仍然可以看到什么我正在尝试做:) 原因为什么我这样做,而不是声明直接颜色的数据源我是aspx-page,是我需要运行一个测试 if(showColors),但我不想要用代码弄乱 html 页面我觉得应该在代码中后面的文件.

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.

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

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

解决方案:

要么将事件冒泡到 ItemCommand,要么处理事件,获取发件人的父级(这是一个 datalistItem 并在其中操作元素.

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

推荐答案

DataList 绑定数据时,AutoPostBack 还未处理,即 ItemCreated 事件中的值仍为原始值.

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.

需要处理下拉控件的SelectedIndexChange事件.

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

这篇关于DropdownList.selectedIndex 始终为 0(是的,我确实有 !isPostBack)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆