Andy
Andy

Reputation: 503

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).

The problem is that when I try to change the item in the dropdown list, nothing happens.

Thanx!

.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

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

Upvotes: 0

Views: 969

Answers (3)

Joel Etherton
Joel Etherton

Reputation: 37543

Your DataValueField never equals Banana. It equals "Value". You're using the wrong selector. You want to use 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.

EDIT:

Since you say the event never gets hit, it's possible the autoeventwireup is failing on the second postback. I'm not sure why it would happen, but I have seen events fail if they are defined in the xml. You may try moving your event wireup from the xml side to the cod behind in the page load event.

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

Upvotes: 2

goenning
goenning

Reputation: 6654

ddlProducts.DataValueField will never be Banana since you set it to "Value" at the BindDictionary method.

Upvotes: 1

jimplode
jimplode

Reputation: 3512

Do you have AutoEventWireup set in the page declaration?

See here: msdn AutoEventWireUp

Upvotes: 0

Related Questions