Note that there are some explanatory texts on larger screens.

plurals
  1. POasp.net : Getting SelectedItem of a DropDownList and retaining list items
    primarykey
    data
    text
    <p>Here's the issue..</p> <p>I have small page which has label, dropdownlist and a submit button.</p> <pre><code>&lt;div&gt; &lt;asp:label id="Message" runat="server"/&gt; &lt;br /&gt; Which city do you wish to look at on hotels for?&lt;br /&gt;&lt;br /&gt; &lt;asp:dropdownlist id="Dropdownlist1" runat="server" EnableViewState="true"&gt; &lt;/asp:dropdownlist&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;input type="Submit" /&gt; &lt;/div&gt; </code></pre> <p>On form load i am inserting items into the dropdownlist and on the button click i am displaying the count of the items in the dropdownlist. Here's the code for that.</p> <p><code> if (Page.IsPostBack) { Message.Text = "You have selected " + Dropdownlist1.Items.Count.ToString(); } else { Message.Text = "You have selected " + Dropdownlist1.Items.Count.ToString(); Dropdownlist1.Items.Add("Madrid"); Dropdownlist1.Items.Add("Chennai"); Dropdownlist1.Items.Add("New York"); } </code></p> <p>Here's the funny part. If i run it directly from the IDE, its working perfectly fine. I get the count as 0 the first time and 3 when i press submit button. I need to run this small code on an existing virtual directory. If i run the same aspx page within that virtual directory, i get count 0 for the for the first time it loads. When i click submit, i get count as 0 and i don't see any items in the dropdownlist, it is getting cleared. I have set viewstate to true so that i remember what was inserted.</p> <p>I am not sure what difference is there when i run it from IDE and when i run it from another virtual directory. I am fairly new to Asp.Net so i have exhuasted all my options here so to find out how a dropdownlist works. Is there a config i am missing here ?. </p> <p>Btw just FYI, I am facing the same issue when i put the DropDownList in a Wizard Control. When run from IDE it is working fine but when i run from the virtual directory its not getting the selected value neither is it remembering the items in the dropdownlist.</p> <p>Any help on this would be appreciated.</p> <hr> <p>sorry :(, couple of more questions</p> <blockquote> <p>According to your code the list only gets populated when it is not a PostBack. Therefore when you click the button the list will be empty.If you dynamically populate the list, the items are not remembered. You must added in each Page_load. The view state will only remember which item was selected.</p> </blockquote> <p>How it does then remember the items when the page is executed directly from the IDE and not remember when i run from a virtual directory. Is there a view state that i might need to set to get it working. The cache setting also did not do much luck. I enabled Trace info, funny thing again :|, tracing is happening when executed directly from the IDE and not from the virual directory. Question again, the child directory's web.config should override the parent web.config right ?</p> <p>Thanks for replying.</p>
    singulars
    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