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