views:

1735

answers:

5

Ok Here's the issue..

I have small page which has label, dropdownlist and a submit button.

<div>
    <asp:label id="Message" runat="server"/> 
        <br />
        Which city do you wish to look at on hotels for?<br /><br />
        <asp:dropdownlist id="Dropdownlist1" runat="server" EnableViewState="true">
        </asp:dropdownlist>
        <br /><br /><br /><br />
        <input type="Submit" />     
</div>

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.

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

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

Any help on this would be appreciated.

A: 

Maybe the page is being cached when in a virtual directory.

Try adding this to the Page_Load

Response.Cache.SetCacheability(HttpCacheability.NoCache)

I think caching can be applied to directories using a config file. So may be this is the difference.

tpower
A: 

Being curious here, If its being cached, shouldn't it remember the items in the dropdownlist ?

vikramjb
A: 

sorry :(, couple of more questions

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.

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 ?

Thanks for replying.

vikramjb
+4  A: 

It sounds like you may have EnableViewState disabled at the page level. Contrary to the other responses, you don't need to repopulate your lists on PostBack if ViewState is enabled.

Try adding the EnableViewState="true" attribute in your page header.

I think it is a bug. If EnableViewState="false" at the page level, and EnableViewState="true" at the control level, ViewState isn't retained for the control like it should be.

If you only want to enable ViewState for certain controls, set EnableViewState="true" at the page level, and then EnableViewState="false" for the controls which you don't want to retain ViewState for. Backwards, I know, but it's the only workaround.

Gordon Bell
A: 

Bingo.. Gordon, that did it.. Thanks a ton for the reply.. I have changed my main page to reflect the viewstate and it is working perfect now :). Thank a ton.. A quick question, if viewstate was the problem, why was it working when i had executed it directly from the IDE and not working from the virtual directory, any idea ?

vikramjb
Good question, and I'm not sure. I'm able to reproduce the problem in the IDE though.
Gordon Bell