简体   繁体   English

OnSelectedIndexChanged似乎不起作用

[英]OnSelectedIndexChanged don't seem to work

I am having some trouble getting my code do what I want it to do, and I would appreciate your help. 我在使代码执行我想做的事情时遇到了一些麻烦,感谢您的帮助。

What I would like to do is: 我想做的是:

From a textbox, I add a name for a product and create a object with that name. 在文本框中,我为产品添加了一个名称,并使用该名称创建了一个对象。

The product object is then added to a Dictionary. 然后将产品对象添加到字典中。

Then, I want to bind this Dictionary to a dropdown list. 然后,我想将此字典绑定到一个下拉列表。

If I change the selected item, I want to display the number of the chosen product (Default as 0 when I create the product object). 如果更改所选项目,则要显示所选产品的编号(创建产品对象时默认为0)。

The problem is that when I try to change the item in the dropdown list, nothing happens. 问题是,当我尝试更改下拉列表中的项目时,什么也没有发生。

Thanx! 谢谢!

.aspx .aspx

    <asp:TextBox ID="productText" runat="server"></asp:TextBox>
    <asp:Button ID="newProductButton" runat="server" OnClick="newProduct_Click" />

<div>
    <asp:DropDownList ID="ddlProducts" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlProducts_SelectedIndexChanged" >
    </asp:DropDownList>
    </div>
    <asp:Label ID="productQuantity" runat="server"></asp:Label>

.aspx.cs .aspx.cs

public partial class Pages_productPage : System.Web.UI.Page
{
    string _productName = string.Empty;



    public Dictionary<string, int> product
    {
        get
        {
            if (Page.Session["product"] == null)
            {
                Dictionary<string, int> product = new Dictionary<string, int>();
                Page.Session["product"] = product;
            }
            return (Dictionary<string, int>)Page.Session["product"];
        }
        set
        {
            Page.Session["product"] = value;
        }
    }

    protected string ProductName
    {
        get { return _productName; }
        set { _productName = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //ddlProducts.Items.Insert(0, new ListItem("Products", "0"));
        }
        productLabel.Text = "Product name";
        newProductButton.Text = "Add product";
        ProductName = productText.Text;


    }

    public void newProduct_Click(object sender, EventArgs e)
    {
        Product prod = new Product(ProductName);
        product.Add(prod.GetName(prod), prod.GetQuantity(prod));
        BindDictionary();
    }

    private void BindDictionary()
    {
        dictonaryRepeater.DataSource = product;
        dictonaryRepeater.DataBind();
        ddlProducts.DataSource = product;
        ddlProducts.DataValueField = "Value";
        ddlProducts.DataTextField = "Key";
        ddlProducts.DataBind();
        //ddlProducts.Items.Insert(0, new ListItem("Products", "0"));

    }

    public void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlProducts.DataValueField == "Banana")
        {
            productQuantity.Text = ddlProducts.SelectedItem.ToString();
            productQuantity.Visible = true;
        }
    }
}

Your DataValueField never equals Banana. 您的DataValueField永远不等于香蕉。 It equals "Value". 它等于“值”。 You're using the wrong selector. 您使用了错误的选择器。 You want to use ddlProducts.SelectedValue. 您要使用ddlProducts.SelectedValue。 And when I'm looking at it again, it looks like you're using value when you really want to be using text. 当我再次查看它时,您似乎真的想使用文本时就在使用价值。

Code suggestion: 代码建议:

public void ddlProducts_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        if ("Banana".Equals(ddlProducts.SelectedItem.ToString(),  StringComparison.OrdinalIgnoreCase) 
        { 
            productQuantity.Text = ddlProducts.SelectedValue.ToString(); 
            productQuantity.Visible = true; 
        } 
    }

** Added the equals bit from something I learned from Mr. Skeet. **从我从Skeet先生那里学到的东西添加了等值点。

EDIT: 编辑:

Since you say the event never gets hit, it's possible the autoeventwireup is failing on the second postback. 由于您说事件从未被触发,因此autoeventwireup可能在第二次回发时失败。 I'm not sure why it would happen, but I have seen events fail if they are defined in the xml. 我不确定为什么会发生这种情况,但是我看到如果在xml中定义事件,则事件将失败。 You may try moving your event wireup from the xml side to the cod behind in the page load event. 您可以尝试在页面加载事件中将事件联结从xml端移到后面的cod。

protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!Page.IsPostBack) 
        { 
            //ddlProducts.Items.Insert(0, new ListItem("Products", "0")); 
        } 
        productLabel.Text = "Product name"; 
        newProductButton.Text = "Add product"; 
        ProductName = productText.Text; 

        ddlProducts.SelectedIndexChanged += ddlProducts_SelectedIndexChanged;
    } 

ddlProducts.DataValueField将永远不会是香蕉,因为您在BindDictionary方法中将其设置为“ Value”。

Do you have AutoEventWireup set in the page declaration? 页面声明中是否设置了AutoEventWireup?

See here: msdn AutoEventWireUp 参见此处: msdn AutoEventWireUp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM