Note that there are some explanatory texts on larger screens.

plurals
  1. POOnSelectedIndexChanged don't seem to work
    primarykey
    data
    text
    <p>I am having some trouble getting my code do what I want it to do, and I would appreciate your help.</p> <p>What I would like to do is:</p> <p>From a textbox, I add a name for a product and create a object with that name.</p> <p>The product object is then added to a Dictionary. </p> <p>Then, I want to bind this Dictionary to a dropdown list. </p> <p>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).</p> <p>The problem is that when I try to change the item in the dropdown list, nothing happens.</p> <p>Thanx!</p> <p>.aspx</p> <pre><code> &lt;asp:TextBox ID="productText" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:Button ID="newProductButton" runat="server" OnClick="newProduct_Click" /&gt; &lt;div&gt; &lt;asp:DropDownList ID="ddlProducts" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlProducts_SelectedIndexChanged" &gt; &lt;/asp:DropDownList&gt; &lt;/div&gt; &lt;asp:Label ID="productQuantity" runat="server"&gt;&lt;/asp:Label&gt; </code></pre> <p>.aspx.cs</p> <pre><code>public partial class Pages_productPage : System.Web.UI.Page { string _productName = string.Empty; public Dictionary&lt;string, int&gt; product { get { if (Page.Session["product"] == null) { Dictionary&lt;string, int&gt; product = new Dictionary&lt;string, int&gt;(); Page.Session["product"] = product; } return (Dictionary&lt;string, int&gt;)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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload