saving gridview data between app runtime, in a static object/class

gal mor 141 Reputation points
2022-10-30T06:43:43.167+00:00

I've asked that question before but will try again.
I'd like to save my current gridview's data in a static var/class so I have the current gridview data stored in it as I run the application.
I dont have a use for it, Its just about practicing on how / if doing that is possible.
thanks!

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,359 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,753 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,051 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,467 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,671 Reputation points
    2022-10-31T13:38:11.537+00:00

    I'm not much familiar with static indeed, and I was able to make a static variable and store the data inside but I'm not quite sure on how to pass that data between the web pages.

    It makes no logical sense to pass a static variable because by definition a static variable is global to the application. Simply set the variable and read it in another page. However, I'm certain the person that assigned this task does not expect you to create a static variable. I assume the task is to create a static class that saves the data which is a totally different concpet.

    To get data in another page, the standard approach is passing a unique key to the page and using the key to look up the data in a database. Usually a querystring is used (HTTP GET) which passes the ID in the URL. For example...

    http://myapp/default.aspx?id=1  
    

    Again, I recommend that you speak with the person that assigned the task as I do not think you understand the assignment and asking the same question on this forums is not going to help until you take the time to understand the fundamentals.

    Static variable example which I'm sure is not the assignment.

    Default.cs

        public class StaticModel  
        {  
            public static string MyStaticVar;  
        }  
      
        public partial class Default : System.Web.UI.Page  
        {       
            protected void Page_Load(object sender, EventArgs e)  
            {  
                StaticModel.MyStaticVar = "Hello World";  
            }  
        }  
    

    Default.aspx

            <div>  
                <a href="GetStaticVar.aspx">View the Static Variable</a>  
            </div>  
    

    GetStaticVar.cs

            protected void Page_Load(object sender, EventArgs e)  
            {  
                Label1.Text = StaticModel.MyStaticVar;  
            }  
    

    GetStaticVar.aspx

            <div>  
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
            </div>  
    

    Reference documentation

    static (C# Reference)

    You might consider going through a few Web Forms tutorials to learn the basics.

    Getting Started with ASP.NET 4.5 Web Forms and Visual Studio 2017

    1 person found this answer helpful.

  2. AgaveJoe 26,671 Reputation points
    2022-10-31T14:29:02.167+00:00

    This forum expects a basic understanding of the technology and the C# programming language. It is very difficult to provide assistance if you do not understand the question you are asking or the answer.

    I also tried to get the data into a static class, but again couldnt find a good source on how to do that

    A static class is simply a class that has at least one static member. Calling the static member does not require a "new" class; an instance.

    Passing data to a static method is done by defining method parameters which can be a simple type like a string or a complex type like a class. Keep in mind, that Implementing a static class has no bearing on fetching data from a database or returning set data to the caller.

        public class MyStaticClass  
        {  
            public static string MyStaticMethod(string data)  
            {  
                return data;  
            }  
        }  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                string results = MyStaticClass.MyStaticMethod("Hello World");  
                Console.WriteLine(results);  
            }  
        }  
    

    aswell as how to populate it among the different webpages.

    Passing data between web pages is extremely well-documented. The most common method is the URL (HTTP GET).

    default.aspx

            <div>  
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  
            </div>  
    

    default.cs

            protected void Button1_Click(object sender, EventArgs e)  
            {  
                string greeting = "Hello World";  
                Response.Redirect($"GetData.aspx?greeting={greeting}");  
            }  
    

    GetData.cs

            protected void Page_Load(object sender, EventArgs e)  
            {  
                Label1.Text = Request.QueryString["greeting"];  
            }  
    

    GetData.aspx

            <div>  
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
            </div>  
    

    Rather than confusing the community, ask the person assigning the task for clarification and guidance. Another option learning common programming patterns by going through a few Web Forms tutorials and reading the C# programming guide when you are not sure what a term means.

    0 comments No comments