Are there ASP.NET Session design patterns?
Are there ways of structuring session storage in ASP? I usually put a string as the key, but I'm thinking of taking more stringent measures.
The issue has come up because I have an application that has to use session heavily for 2 reasons:
- The application has to perform operations on data before presenting it to the user.
- The application has to validate some choices before writing changes.
My strategy has been to build lists and store them in session on page load.
I'm running into several problems with this. I keep getting reports of things being 'entered for users,' namely that one of the lists I build and store in session shows values entered before they've had a chance to add them. I'm not sure why this keeps happening, given that I rebuild the lists 开发者_StackOverflow社区stored in session on each load. I suspect there is a lot of back button pushing.
Are there ways to design the session storage so that the lists stored are unique to that instance of the page? Or, failing that, a way of designing session storage so that it is always unique to that page?
Generally, you use session for data that can be shared between pages, and is specific to a user.
What you're describing is not a good use of session. Consider what happens if the user happens to open two pages in separate tags - the session data from the 2nd tab will overwrite that from the first, and you're on your way to data corruption.
There is nothing wrong with manipulating data before presenting it to the user, and this is usually performed in your business layer. If the manipulation is computationally expensive, then consider some sort of caching strategy.
And as @mikek3332002 says, use the build-in validation tools. Again, there is rarely a reason to use the session as part of the validation.
There is functionality called viewstate which save post-back data per page, on the client's page.
After Editing
Based off the comments it sounds like you more want something that either returns rows where the key is shared amongst 2 tables (SQL's inner join
) or have the values of one control depend on the property of another control.
The second one is used like (adapted from http://msdn.microsoft.com/en-us/magazine/cc163862.aspx)
public DataSet GetData(int ID) { ... }
<asp:ObjectDataSource ID="odsDataSource" runat="server" TypeName="DataDepend" SelectMethod="GetData">
<SelectParameters>
<asp:ControlParameter Name="ID" Type="int"
ControlID="ddlList1"
PropertyName="SelectedValue"></asp:ControlParameter>
</SelectParameters>
</asp:ObjectDataSource>
精彩评论