how to save gridview data as a static object which doesnt rely on page's state

gal mor 141 Reputation points
2022-10-27T07:13:51.417+00:00

hey everyone. the title is basically my question. how can I save my gridview data in a static manner so even if I end the session/state, ill still have the value stored as static data ? thanks

Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-10-27T09:12:25.357+00:00

    Hi @gal mor ,
    You can use View State, which is the way to preserve Page and Controls values between round trips. It is a page-level state management technique.
    https://learn.microsoft.com/en-us/previous-versions/dotnet/articles/ms972976(v=msdn.10)?redirectedfrom=MSDN
    These are the main features of view state,

    • Retains the value of the Control after post-back without using a session.
    • Stores the value of Pages and Control Properties defined in the page.
    • Creates a custom View State Provider that lets you store View State Information in a SQL Server Database or in another data store.

    According to your previous post, are you trying to set EnableViewState to false? The documentation states:
    https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview?redirectedfrom=MSDN&view=netframework-4.8#data-operations

    The GridView control is re-created on postback based on the information that is stored in ViewState. If the GridView control includes a TemplateField or a CommandField with the CausesValidation property set to true, then the EnableViewState property must also be set to true to ensure that concurrent data operations, such as updates and deletes, apply to the appropriate row.

    Note that the if (!IsPostBack) needs to be removed when using EnableViewState="false".

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-10-31T15:39:48.277+00:00

    this is not a viable option unless the website allows only one user. a typical website site can have more than one user access the same page. but a static only holds one value. store data between pages is typically done with a session manager. the data is strode by a key, and the key is stored in a cookie which is unique per user.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.