2

我不想在这里再放一个,但是这种类型的其他 20 个问题已经用“set AutoPostBack="true"”回答了,我已经完成了。我相信我的问题更多在于我在 DropDownList 中设置项目的方式,但我是 ASP.NET 的新手,所以我不确定正确的解决方案是什么。

这是我的控制:

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

这是我在页面的代码隐藏中设置它的地方:

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

最后是我的听众:

protected void DrpProduct_SelectedIndexChanged(object sender, EventArgs eArgs) 
{
  //code omitted. I have a breakpoint here that never gets hit anyways.
}

它们正确渲染和显示,但是当我使用下拉菜单时未命中 OnSelectedIndexChanged 事件。我已经读过,如果没有在页面生命周期的适当阶段以这种方式将项目添加到下拉列表中,可能会出现问题,但我已经在多个其他阶段尝试过,但没有效果。出于特定原因,我这样做(而不是数据绑定对象等),所以想让这段代码为我工作。

编辑:根据要求,这是我的 ProductRepository 代码:

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

4 回答 4

2

好吧,想通了。这实际上是我的一个非常愚蠢的错误。

我的 DropDownList 只是挂在页面上,而不是包含在<form>. 不知道这会否定 SelectedIndexChanged 事件。因此,将 .aspx 文件中的代码更改为:

<form ID="ProductForm" runat="server">
  <asp:DropDownList .... />
</form>
于 2013-10-15T23:25:47.550 回答
0

Make sure you've wired up the EventHandler correctly.

Try switching to Designer mode in Visual Studio and double-clicking on the DropDownList to make sure that the CodeBehind opens up to the actual DrpProduct_SelectedIndexChanged method.

This is always a good way to test your code and make sure that everything is wired up.

You can also try wiring up your EventHandler programmatically like this...

DrpProduct.Click += new eventHandler(DrpProduct_SelectedIndexChanged);
于 2013-10-15T23:11:40.327 回答
0

以下是经过测试和工作的。我很想知道你是如何定义的Products?为简洁起见,我省略了aspx代码。

public partial class _Default : Page
{
    List<Product> products = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            products = new List<Product> 
            { 
                new Product{ Name = "ProductOne" },
                new Product{ Name = "ProductTwo" }
            };

            if (products.Count > 0)
                foreach (var product in products)
                    DrpProduct.Items.Add(new ListItem(product.Name, product.Name));
        }
    }

    protected void DrpProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        // break point in here is getting hit
    }

}

class Product
{
    public string Name { get; set; }
}

Product尝试用像我这样的硬编码类型列表替换您从存储库中获取的数据,看看您是如何进行的。这将证明你的设置确实是正确的,问题可能出在其他地方。

于 2013-10-15T22:40:55.373 回答
0

此外,您可能忘记将控件的 AutoPostBack 设置为 true ......我不会承认这是否是我的解决方案 8)

于 2017-06-25T06:40:32.410 回答