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

问题描述

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

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

Got a asp.net page which contains aDatalist. Inside this datalist, thereis a template containing adropdownlist and each time thedatalist is filled with an item, aItemCreatedCommand is called. TheitemCreatedCommand is responsible fordatabinding the dropdownlist.

我认为问题出在这里,那个
我正在使用ItemCreatedCommand来
填充它 - 但奇怪的东西
是如果我选择颜色绿色,
该页面将会autopostback,我将
看到下拉列表仍然在
颜色绿色,但是当尝试使用
它是SelectedIndex,我总是得到0 ...

I think the problem lies here, thatI'm using ItemCreatedCommand topopulate it - but the strange thingsis that if I choose the color "green",the page will autopostback, and I willsee that the dropdown is still on thecolor green, but when trying to useit'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 aspx页面,是我需要运行一个
测试if(showColors),但我不想要
混乱html页面与代码
,我觉得应该在代码
背后文件。

I've narrowed down the code a bit forviewing, but still you can see whatI'm trying to do :) The reason forwhy I'm doing this, and not declaringthe datasource for the colors directlyi aspx-page, is that I need to run atest if(showColors), but I do not wantto clutter up the html-page with codethat I feel should be in the codebehind-file.

编辑:尝试更改
SelectedIndexChange - 我现在有一个
逻辑混乱 -
如何我要修改
datalist里面的元素吗?因为,据我所知,我
没有办法检查
中的哪一个数据列表中的这个
特定的下拉列表属于...
或?我要尝试几种方式
,看看我最终的结果;)但是做
请发表你对这个
问题的想法:)

After trying to alterSelectedIndexChange - I'm having a"logical" confusion in my head now -how am I to alter elements inside thedatalist? Since, as far as I know - Ido not have any way to check which ofthe items in the datalist thisparticular dropdownlist belongs to...Or? I'm going to try out a few waysand see what I end up with ;) But doplease post your thoughts on thisquestion :)

解决方案:

将事件气泡到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;
        }


推荐答案

当DataLis t是数据绑定的,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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 04:52