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

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

问题描述

(向下滚动后的底部找到解决办法。)

得到其含有一个asp.net页
DataList控件。这里面的DataList,有
由含有模板的
的dropdownlist并且每次
数据列表充满了一个项目,一个
ItemCreatedCommand被调用。该
itemCreatedCommand负责
数据绑定DropDownList的。

我觉得问题就出在这里,那
我使用ItemCreatedCommand到
填充它 - 但奇怪的事情
是,如果我选择的颜色绿色,
页面会自动回,我就
看到的下拉仍然在
色绿,而是试图用时
它的selectedIndex,我总是得到0 ...

 保护无效DataListProducts_ItemCreatedCommand(对象
    源,DataListItemEventArgs E) VAR的itemId =(字符串)DataListProducts.DataKeys [e.Item.ItemIndex]
 VAR项目= itemBLL.GetFullItem(的itemId); VAR DropDownListColor =(DropDownList的)e.Item.FindControl(DropDownListColor); //也试过用:
 //如果(!的IsPostBack){ DropDownListColor.DataSource = item.ColorList;
 DropDownList.Color.Databind(); //}结束!的IsPostBack)    Label1.test = DropDownListColor.SelectedIndex.toString();
 //< - 这始终是0! * GRR *

我已经收窄code位为
查看,但你仍然可以看到什么
我试图做:)原因
为什么我这样做,并没有宣布
数据源直接的颜色
我ASPX页面,就是我需要运行
如果测试(showColors),但我不希望
杂波与code中的HTML页
我觉得应该是在code
后面的文件。

编辑:试图改变后
SelectedIndexChange - 我有一个
在我的脑袋逻辑现在的困惑 -
我怎么改变里面的元素
DataList控件?因为,据我知道 - 我
没有任何的方法来检查,其中
在此DataList控件的项目
特别的DropDownList属于...
要么?我要去尝试一些方法
,看看我结束了;)但是做
请发表您对这个想法
问题:)

解决方案:

无论气泡的事件来ItemCommand,或处理的情况下,获得的发送者的父(这是一个的DataListItem并在那里操纵元件

 保护无效DropDownListColor_SelectedIndexChanged(对象发件人,EventArgs的发送)
        {
            DropDownList的dropDownListColor =(DropDownList的)寄件人;
            的DataListItem的DataListItem =(的DataListItem)dropDownListColor.Parent;            VAR项目=项目[dataListItem.ItemIndex]
            VAR颜色= item.ItemColor [dropDownListColor.SelectedIndex]            VAR LabelPrice =(标签)dataListItem.FindControl(LabelPrice);
            LabelPrice.Text = color.Price;
        }


解决方案

在DataList控件是数据绑定,将AutoPostBack尚未处理,即在ItemCreated事件中的值仍然是原来的值。

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

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

解决方案

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.

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

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