当前位置: 首页 > 编程语言 > C#

DropdownList.selectedIndexisalways0(yes,Ido!isPostBack)share

时间:2023-04-11 10:50:46 C#

DropdownList.selectedIndexisalways0(yes,Ido!isPostBack)(向下滚动到帖子底部找到解决方案.)有一个包含数据列表的asp.net页面。在此数据列表中,有一个包含下拉列表的模板,每次用项目填充数据列表时,都会调用ItemCreatedCommand。itemCreatedCommand负责对下拉列表进行数据绑定。我认为问题出在这里,我正在使用ItemCreatedCommand来填充它-但奇怪的是,如果我选择颜色“绿色”,页面将自动回复并且我看到下拉列表仍然是绿色,但是当尝试使用它时SelectedIndex,我总是得到0...protectedvoidDataListProducts_ItemCreatedCommand(对象源,DataListItemEventArgse)varitem=itemBLL.GetFullItem(itemId);varDropDownListColor=(DropDownList)e.Item.FindControl("DropDownListColor");//也尝试过://if(!isPostBack){DropDownListColor.DataSource=item.ColorList;DropDownList.Color.Databind();//}End!isPostBack)Label1.test=DropDownListColor.SelectedIndex.toString();//<-这总是0!*grr*我已经缩小了代码以供查看,但您仍然可以看到我正在尝试做的事情:)我这样做的原因,而不是直接声明aspx页面颜色的数据源,因为我需要运行一个测试if(showColors),但我不想将代码放在html页面上,我认为它应该在代码隐藏文件中。编辑:在尝试更改SelectedIndexChange之后-我现在脑子里有一个“逻辑”混乱-如何更改Dataist中的元素?因为据我所知-我没有办法检查这个特定下拉列表属于数据列表中的哪些项目......或者?我要尝试一些事情,看看我最终会得到什么;)但请发表你对这个问题的想法:)解决方案:要么将事件冒泡到ItemCommand,要么处理事件,获取发件人父级(这是一个datalistItem并在那里操作元素)。protectedvoidDropDownListColor_SelectedIndexChanged(objectsender,EventArgse){DropDownListdropDownListColor=(DropDownList)sender;DataListItemdataListItem=(DataListItem)dropDownListColor.Parent;varitem=items[dataListItem.ItemIndex];;varLabelPrice=(Label)dataListItem.FindControl("LabelPrice");LabelPrice.Text=color.Price;当DataList是数据绑定的时候,AutoPostBack还没有做处理,即ItemCreated事件中的值还是原来的值。您需要处理下拉控件的SelectedIndexChange事件。关于你的第二个问题:我建议你从下拉列表中删除AutoPostBack,添加一个“更新”按钮,并更新按钮的Click事件中的数据。该按钮可以保存Command和CommandArgument值,因此很容易与数据库记录相关联。一些MSDN链接与冒泡的C#示例http://msdn.microsoft.com/en-us/library/system.web.ui.control.onbubbleevent.aspxhttp://msdn.microsoft.com/en-us/library/aa719644(VS.71).aspxhttp://msdn.microsoft.com/en-us/library/aa720044(VS.71).aspx谢谢解答以上是C#学习教程:DropdownList.selectedIndex就可以了是0共享的所有内容(是的,我有!isPostBack)。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——protectedvoidddlOnSelectedIndexChanged(objectsender,EventArgse){如果(ViewState["Colors"]!=null){FillColors(ViewState["Colors"].ToString());}DropDownListdropDownListColor=(DropDownList)sender;DataListItemdataListItem=(DataListItem)dropDownListColor.Parent;图片image=(Image)dataListItem.FindControl("mdlImage");标签ProductCode=(Label)dataListItem.FindControl("lblprdCode");标签ProductName=(Label)dataListItem.FindControl("lblProdName");DropDownListddlQuantity=(DropDownList)dataListItem.FindControl("ddlQuantity");标签ProductPrice=(标签l)dataListItem.FindControl("lblProdPrice");标签TotalPrice=(Label)dataListItem.FindControl("lblTotPrice");//LabelProductPrice=(Label)dataListItem.FindControl("lblProdPrice");}catch(Exceptionex){}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:

最新推荐
猜你喜欢