的DropDownList的SelectedIndexChanged没有发射(是的,的AutoPostBack ="真") [英] DropDownList SelectedIndexChanged not firing (yes, AutoPostBack="true")

查看:112
本文介绍了的DropDownList的SelectedIndexChanged没有发射(是的,的AutoPostBack ="真")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我讨厌把这里这些了一个又一个,但这种类型的其他20个问题已经回答是:设置的AutoPostBack =真正的,这是我'已经已经完成。我相信我的问题,更多的是我设置在DropDownList项目的方式,但我在ASP.NET是新的,所以我不知道的妥善解决是什么。

I hate to put another one of these up here, but the 20 other questions of this type have been answered with "set AutoPostBack="true"", which I've already done. I believe my problem is more in the way I'm setting the items in the DropDownList, but I'm new at ASP.NET, so I'm not sure what the proper solution is.

下面是我的控制:

<asp:DropDownList ID="DrpProduct" CssClass="input-xxlarge"  runat="server"    AutoPostBack="true" EnableViewState="true"     OnSelectedIndexChanged="DrpProduct_SelectedIndexChanged" ViewStateMode="Enabled" />

和这里的地方,我在页面的code-背后将其设置

protected void Page_Init(object sender, EventArgs e) { Repository = new ProductRepository(); Products = Repository.List(); if (Products.Any()) foreach (Product product in Products) { DrpProduct.Items.Add(new ListItem(product.Name, product.Name)); } }

And finally my listener:

最后我监听器:

They render and display properly, but the OnSelectedIndexChanged event is not being hit when I use the dropdown. I've read that adding items to the dropdown this way can be problematic if not done at the proper stage of the page's lifecycle, but I've tried it on multiple other stages to no effect. I'm doing it this way (rather than data bound objects, etc) for a specific reason, so would like to make this code work for me. 

他们渲染和显示正常,但是当我使用的下拉列表中OnSelectedIndexChanged事件没有被击中。我读过,将项目添加到下拉这种方式是有问题的,如果不是在页面生命周期的适当阶段完成,但我已经试过了多个其他阶段没有效果。我做的这种方式(而不是数据绑定的对象等),为特定的原因,所以想使这个code为我工作。

By request, here is my ProductRepository code:

编辑:按要求,这里是我的ProductRepository code:

public class ProductRepository { private IList<Product> Products { get; set; } public ProductRepository() { Products = new List<Product> { new Product("Grand Theft Auto V", "A video game that lets you kill hookers.", 59.99m), new Product("Fallout 4", "lol u wish", 99.99m), new Product("XCOM: Enemy Unknown", "Probably one of the better games you'll ever play.", 39.99m), new Product("The Bureau: XCOM Declassified", "The game NO ONE asked for. Or wanted.", 59.99m), new Product("Rome 2: Total War", "There'd better be phalanxes.", 59.99m) }; } public IList<Product> List() { return Products; } }


推荐答案

好吧,理解了它。它实际上是我的一个pretty愚蠢的错误。

Alright, figured it out. It was actually a pretty stupid mistake on my part.

我的DropDownList只是挂在网页上,而不是被包含在一个&LT;形式为GT; 。不知道这会否定SelectedIndexChanged事件。所以,改变了我的code在我的.aspx文件:

My DropDownList was just hanging out on the page, rather than being contained within a <form>. Wasn't aware that would negate the SelectedIndexChanged event. So, changed my code in my .aspx file to:

<form ID="ProductForm" runat="server">
  <asp:DropDownList .... />
</form>

这篇关于的DropDownList的SelectedIndexChanged没有发射(是的,的AutoPostBack =&QUOT;真&QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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