tags:

views:

52

answers:

3

Hi,

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

Do you have AutoEventWireup set in the page declaration?

See here: msdn AutoEventWireUp

jimplode
He is wiring it up manually.
Dismissile
Yes, i have AutoEventWireup="true"
Andy
When i say nothing happends i mean that i never hit my ddlProducts_SelectedIndexChanged method.
Andy
@Dismissile: AutoEventWireup="true" means that to wire up an event all you have to do is name the event in your .aspx page, like for a DropDownList:<asp:DropDownList id="ddl1" runat="server" SelectedIndexChanged="ddl1_SelectedIndexChanged" />and in your code behind:protected void ddl1_SelectedIndexChanged(object sender, EventArgs e){}Without the autoeventwireup, you would have to subscribe to the event manually (wire up), for example, in your Page constructor:this.ddl1.SelectedIndexChanged += new EventHandler(ddl1_SelectedIndexChanged)
jimplode
+2  A: 

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;
    } 
Joel Etherton
Tryed that, no change...
Andy
@Andy - You're only binding the ddlProducts on the click event. The selectedIndexChanged event is probably firing, but because you're not binding the ddlProducts on the subsequent load, it doesn't actually change from index 0. Try binding ddlProducts in the Page_Load if it IS a postback.
Joel Etherton
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; BindDictionary(); }
Andy
That looked a mess but i added a call to BindDictionary in the Page_Load but that did not help.
Andy
@Andy - is it performing the postback at all on the change? does it go through the load and just not enter your indexchanged method?
Joel Etherton
@Joel - Yes, it goes through the load on every change but not the indexchanged method.
Andy
+1  A: 

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

Guilherme Oenning
Ok, i can see that but the method is never hit
Andy