0

I'm a beginner at ASP.NET, but I'm trying to fix a bug in an application written by someone else: a drop-down list's selection is not retained across a postback.

Here are what I believe are the relevant parts of the code:

<asp:DataList ... OnItemDataBound="PopulateDropDownList">
    ...
    <FooterTemplate>
        <asp:DropDownList ... AutoPostBack="true" OnSelectedIndexChanged="DoSomething"/> 
    </FooterTemplate>                               
</asp:DataList>

I believe I could store the current selection in the session, a static variable or somewhere else, but this seems more like a work-around then a solution.

Sam
  • 40,644
  • 36
  • 176
  • 219

4 Answers4

1

Usually you can set the EnableViewState to "true" - as below:

<asp:DropDownList ... EnableViewState="true" ...>
</asp:DropDownList>`

But I am not 100% sure if it works the same way inside a DataList, but I am guessing it should.

You can learn more about the view state from Understanding ASP.NET View State.

Make sure you only enable the ViewState for the controls you absolutely need, otherwise you would run into memory issues. From the above source (emphasis mine):

The EnableViewState property is defined in the System.Web.UI.Control class, so all server controls have this property, including the Page class. You can therefore indicate that an entire page's view state need not be saved by setting the Page class's EnableViewState to False. (This can be done either in the code-behind class with Page.EnableViewState = false; or as a @Page-level directive - <%@Page EnableViewState="False" %>.

Chait
  • 1,052
  • 2
  • 18
  • 30
  • The designer indicates that this is `true` by default. I don't have it set to `false`. – Sam Jan 08 '13 at 01:39
  • @Sam: For page level, yes, the `EnableViewState` is true by default, but for individual controls you have to set it explicitly. Please see my edit. Let me know if you are still having trouble. – Chait Jan 08 '13 at 01:48
  • @Sam: Can you also try setting the EnableViewState for ` – Chait Jan 08 '13 at 02:38
  • (Sorry; I removed my last comment.) – Sam Jan 08 '13 at 02:40
  • Explicitly enabling the view state on the `DropDownList` did not work. However, when I also explicitly *disabled* the view state on the page, it *did* work. – Sam Jan 08 '13 at 02:41
  • @Sam: So you have the ViewState for Dropdownlist set to true and set the Page to false, right? I guess my answer should have read "You **should** set the page to false". Anyway, glad to see you got it working. Happy coding! – Chait Jan 08 '13 at 02:46
  • Yes; that's right. However, disabling view state for the entire page breaks other functionality. (This is probably because I'll have to re-enable it for the specific child controls that need it.) Is there a way to do this without first disabling it for the page? – Sam Jan 08 '13 at 03:41
  • @Sam: The reason for disabling the page's view state is so that it will **not** keep the view state for the controls that we don't need to retain after post-backs, thus reducing the overhead (which is critical for pages that have heavy controls). It's always better to keep the view state enabled **only** when needed. As per any other way of doing it, I am not aware of any. – Chait Jan 08 '13 at 04:03
0

First check Page view state is set to True of False. Including EnableViewState="true" will definitely serve your purpose here, you need not to save the selection in session etc.

Umesh
  • 2,704
  • 19
  • 21
  • This worked in a very simple test page that I set up, but in the actual page, which is very large, this did not work. – Sam Jan 08 '13 at 05:26
  • Nah; it's almost 1000 lines long and it's from a commercial project. – Sam Jan 09 '13 at 21:41
0

In the code-behind, doing the data-binding in the page's Init event rather than Load event works around the problem. However, a disadvantage of this is some control values are not populated during Init.

Sam
  • 40,644
  • 36
  • 176
  • 219
0

A colleague pointed out that the Page_Load method was re-binding the DataList even if the current request was a post-back. The problem was resolved by changing this to only bind the data to the DataList if the request is not a post-back.

This seems to be the root cause of the problem, so I think this is the best solution.

Sam
  • 40,644
  • 36
  • 176
  • 219