DropdownList.selectedIndex总是0(是的,我有!isPostBack) [英] DropdownList.selectedIndex always 0 (yes, I do have !isPostBack)

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

问题描述

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

有一个包含
的asp.net页面数据列表。在这个datalist里面,
是一个包含
下拉列表的模板,每次
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来
填充它 - 但奇怪的东西
是如果我选择颜色绿色,
该页面将会autopostback,我将
看到下拉列表仍然在
颜色绿色,但是当尝试使用
它是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* 

我缩小了代码,有一点
查看,但仍然你可以看到
我试图做什么:)
的原因为什么我这样做,而不是直接声明
数据源的颜色直接
i aspx页面,是我需要运行一个
测试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 - 我现在有一个
逻辑混乱 -
如何我要修改
datalist里面的元素吗?因为,据我所知,我
没有办法检查
中的哪一个数据列表中的这个
特定的下拉列表属于...
或?我要尝试几种方式
,看看我最终的结果;)但是做
请发表你对这个
问题的想法:)

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


推荐答案

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

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