Dropdown listbox and auto-postback problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Robel Tareke

    Dropdown listbox and auto-postback problem

    I have a dropdown listbox whose datasource is a dictionary.
    When I have added items to the list and try to select an item from the list, the item I selected does not get the focus. And this comes when I changed the property autopostback to true.
    Here are some of the codes which are relevant to it.
    Code:
     
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    LoadList();
            }
    private void LoadList()
            {
                cbxProduct.DataSource = Supplies;
                if (!IsPostBack)
                { 
                    Supplies.Add("Banana", 1);
                    Supplies.Add("Annana", 1);
                    cbxProduct.DataTextField = "Key";
                    cbxProduct.DataValueField = "Value";
                }
                cbxProduct.DataBind();
            
            }
            public Dictionary<string, int> Supplies
            {
                get
                {
                    if (Page.Session["supplies"] == null)
                    {
                        Dictionary<string, int> supplies = new Dictionary<string, int>();
                        Page.Session["supplies"] = supplies;
                       
                    }
                    return (Dictionary<string, int>)Page.Session["supplies"];
                }
                set
                {
                    Page.Session["supplies"] = value;
                }
    
            }
    Last edited by jhardman; Oct 27 '10, 03:49 PM. Reason: Accidentally posted in the classic asp forum instead of the asp.net forum. Moved.
  • Bassem
    Contributor
    • Dec 2008
    • 344

    #2
    Hi, shouldn't line #9 be moved under the if-statement on the top of the DataBind Method? Also, why you double check the IsPostBack.

    I think when you set the property autopostback to true the ComboBox will be reset to have its original datasource, because of the post back.

    Comment

    Working...