Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. can we store viewstate in masterpage?

can we store viewstate in masterpage?

Scheduled Pinned Locked Moved ASP.NET
question
27 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Abhijit Jana

    Suresh Dayma wrote:

    when i navigate from one page to another then, I just want to retain the customer name across multiple pages without having to interact with the database.

    For that you need to use Session insted of ViewState. Fetch the data first time from DB and put it into session. Now you can access them any where from your site. Following code is used for storing a value to session Collapse

      //Storing UserName in Session
       Session\["CustomerName"\] = dbUserName;
    

    Now, let see how we can retrieve values from Session Collapse

    //Check weather session variable null or not
    if (Session["CustomerName"] != null)
    {
    //Retrieving UserName from Session
    lblWelcome.Text = "Welcome : " + Session["CustomerName"];
    }
    else
    {
    //Do Something else
    }

    Hope this will help you. If you want to know more about the session, please read this one : Exploring Session in ASP.Net[^]

    Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

    N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #6

    Abhijit Jana wrote:

    lblWelcome.Text = "Welcome : " + Session["CustomerName"];

    This won't compile. Adding a string and object is invalid. You need ToString().

    lblWelcome.Text = "Welcome : " + Session["CustomerName"].ToString();

    :)

    Best wishes, Navaneeth

    A 1 Reply Last reply
    0
    • N N a v a n e e t h

      Abhijit Jana wrote:

      lblWelcome.Text = "Welcome : " + Session["CustomerName"];

      This won't compile. Adding a string and object is invalid. You need ToString().

      lblWelcome.Text = "Welcome : " + Session["CustomerName"].ToString();

      :)

      Best wishes, Navaneeth

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #7

      My Bad. Thanks for correcting me !!:thumbsup:

      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      S 1 Reply Last reply
      0
      • S Suresh Pirsquare

        Currently, i have used the sessions to store this. but when the user tries to open it in a new tab then this does not work. Example I have a gridview having multiple customers. if i open multiple customer details on multiple tabs then it will overwrite the customer details. so it does not make sense to store customer details in Session.

        Everything Is Possible!

        A Offline
        A Offline
        Abhijit Jana
        wrote on last edited by
        #8

        Suresh Dayma wrote:

        but when the user tries to open it in a new tab then this does not work.

        Are you talking about browser new Tab ?

        Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

        S 2 Replies Last reply
        0
        • S Suresh Pirsquare

          Currently, i have used the sessions to store this. but when the user tries to open it in a new tab then this does not work. Example I have a gridview having multiple customers. if i open multiple customer details on multiple tabs then it will overwrite the customer details. so it does not make sense to store customer details in Session.

          Everything Is Possible!

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #9

          I think you need to use Database. Once it is modified, just store it into database and from any other tab / in any other browser in the world, it will be shown updated. No need for ViewState / Session etc. ;) If this is your temporary storage, u might use temporary database tables as well. :rose:

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          My Latest Articles-->** Simplify Code Using NDepend
          Basics of Bing Search API using .NET
          Microsoft Bing MAP using Javascript

          S 1 Reply Last reply
          0
          • A Abhijit Jana

            My Bad. Thanks for correcting me !!:thumbsup:

            Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

            S Offline
            S Offline
            Suresh Pirsquare
            wrote on last edited by
            #10

            is there any workaround for this issue?

            Everything Is Possible!

            A 1 Reply Last reply
            0
            • A Abhijit Jana

              Suresh Dayma wrote:

              but when the user tries to open it in a new tab then this does not work.

              Are you talking about browser new Tab ?

              Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

              S Offline
              S Offline
              Suresh Pirsquare
              wrote on last edited by
              #11

              yes, you are correct

              Everything Is Possible!

              1 Reply Last reply
              0
              • S Suresh Pirsquare

                is there any workaround for this issue?

                Everything Is Possible!

                A Offline
                A Offline
                Abhishek Sur
                wrote on last edited by
                #12

                I have already suggested one workaround I guess... See, Viewstate is stored in page (So Rejected) Session is stored in server but for a single session (Rejected as IE opens a new session for each tab) Application is User Independent. Cookie ... Domain specific. Its possible to use this, But I dont like it as client can easily clear that. Also if is browser specific. Say one person opens the same site once in IE another in Mozilla.. Cookies will be different, (Hence Rejected) So there is only one option. Just change it globally, so that anywhere the change is reflected. Hope you got it. cheers. :rose:

                Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                My Latest Articles-->** Simplify Code Using NDepend
                Basics of Bing Search API using .NET
                Microsoft Bing MAP using Javascript

                S 1 Reply Last reply
                0
                • A Abhishek Sur

                  I have already suggested one workaround I guess... See, Viewstate is stored in page (So Rejected) Session is stored in server but for a single session (Rejected as IE opens a new session for each tab) Application is User Independent. Cookie ... Domain specific. Its possible to use this, But I dont like it as client can easily clear that. Also if is browser specific. Say one person opens the same site once in IE another in Mozilla.. Cookies will be different, (Hence Rejected) So there is only one option. Just change it globally, so that anywhere the change is reflected. Hope you got it. cheers. :rose:

                  Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                  My Latest Articles-->** Simplify Code Using NDepend
                  Basics of Bing Search API using .NET
                  Microsoft Bing MAP using Javascript

                  S Offline
                  S Offline
                  Suresh Pirsquare
                  wrote on last edited by
                  #13

                  I really appreciate your help. but still i have to make changes in all the content pages(i.e. globally) right. Isn't there way by which i have to make changes in the masterpage itself?

                  Everything Is Possible!

                  A 2 Replies Last reply
                  0
                  • S Suresh Pirsquare

                    I really appreciate your help. but still i have to make changes in all the content pages(i.e. globally) right. Isn't there way by which i have to make changes in the masterpage itself?

                    Everything Is Possible!

                    A Offline
                    A Offline
                    Abhishek Sur
                    wrote on last edited by
                    #14

                    You are most welcome.. Dont forget to click "good answer" if it actually helped you and also to mark that as answer for future reference. ;)

                    Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                    My Latest Articles-->** Simplify Code Using NDepend
                    Basics of Bing Search API using .NET
                    Microsoft Bing MAP using Javascript

                    1 Reply Last reply
                    0
                    • A Abhishek Sur

                      I think you need to use Database. Once it is modified, just store it into database and from any other tab / in any other browser in the world, it will be shown updated. No need for ViewState / Session etc. ;) If this is your temporary storage, u might use temporary database tables as well. :rose:

                      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                      My Latest Articles-->** Simplify Code Using NDepend
                      Basics of Bing Search API using .NET
                      Microsoft Bing MAP using Javascript

                      S Offline
                      S Offline
                      Suresh Pirsquare
                      wrote on last edited by
                      #15

                      I dont think this is the solution.

                      Everything Is Possible!

                      A 1 Reply Last reply
                      0
                      • A Abhijit Jana

                        Suresh Dayma wrote:

                        but when the user tries to open it in a new tab then this does not work.

                        Are you talking about browser new Tab ?

                        Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                        S Offline
                        S Offline
                        Suresh Pirsquare
                        wrote on last edited by
                        #16

                        Hi abhijit, is there any work around for this issue?

                        Everything Is Possible!

                        1 Reply Last reply
                        0
                        • S Suresh Pirsquare

                          I dont think this is the solution.

                          Everything Is Possible!

                          A Offline
                          A Offline
                          Abhishek Sur
                          wrote on last edited by
                          #17

                          Then please let me clear this. If you open a new browser tab, it doesnt allow you to access any data from other tab because of security issues. Mozilla retains its session data for every tab. So in case of mozilla, it do work in multiple tabs. But in IE it doesnt. One thing that you might use is Cookie. If you call a server for same domain, you can use it to send data. Say you call www.xyz.com it can create cookie data in browser based on the address. So that after a certain while or at the same instant if a new browser is opened and same address is navigated, you will find the data from cookie easily. Say you write like this : Response.Cookies["txtCookie"].Value = this.txtcookie.Text; Response.Cookies["txtCookie"].Expires = DateTime.Now.AddDays(1); Now from another browser instance if you call string txtCookie = Request.Cookies["txtCookie"].Value; it will give you appropriate data. By this way you can send data to different browser instances. :cool:

                          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                          My Latest Articles-->** Simplify Code Using NDepend
                          Basics of Bing Search API using .NET
                          Microsoft Bing MAP using Javascript

                          1 Reply Last reply
                          0
                          • S Suresh Pirsquare

                            I really appreciate your help. but still i have to make changes in all the content pages(i.e. globally) right. Isn't there way by which i have to make changes in the masterpage itself?

                            Everything Is Possible!

                            A Offline
                            A Offline
                            Abhishek Sur
                            wrote on last edited by
                            #18

                            No you can do that in Masterpage as well. Just write your code in masterpage page_load and it will work perfectly. :)

                            Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                            My Latest Articles-->** Simplify Code Using NDepend
                            Basics of Bing Search API using .NET
                            Microsoft Bing MAP using Javascript

                            S 1 Reply Last reply
                            0
                            • A Abhishek Sur

                              No you can do that in Masterpage as well. Just write your code in masterpage page_load and it will work perfectly. :)

                              Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                              My Latest Articles-->** Simplify Code Using NDepend
                              Basics of Bing Search API using .NET
                              Microsoft Bing MAP using Javascript

                              S Offline
                              S Offline
                              Suresh Pirsquare
                              wrote on last edited by
                              #19

                              No dear, it does not work.

                              Everything Is Possible!

                              A 1 Reply Last reply
                              0
                              • S Suresh Pirsquare

                                No dear, it does not work.

                                Everything Is Possible!

                                A Offline
                                A Offline
                                Abhishek Sur
                                wrote on last edited by
                                #20

                                Tell me what you did so far. Just do like this : write Response.Cookies["txtCookie"].Value = myid; Response.Cookies["txtCookie"].Expires = DateTime.Now.AddDays(1); inside your masterpage, and it will be accessible to any page that inherits masterpage (also normal pages) I have done this a lot of times, and it worked perfectly... To request the cookie use Request.Cookies["txtCookie"] :-D

                                Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                                My Latest Articles-->** Simplify Code Using NDepend
                                Basics of Bing Search API using .NET
                                Microsoft Bing MAP using Javascript

                                S 1 Reply Last reply
                                0
                                • A Abhishek Sur

                                  Tell me what you did so far. Just do like this : write Response.Cookies["txtCookie"].Value = myid; Response.Cookies["txtCookie"].Expires = DateTime.Now.AddDays(1); inside your masterpage, and it will be accessible to any page that inherits masterpage (also normal pages) I have done this a lot of times, and it worked perfectly... To request the cookie use Request.Cookies["txtCookie"] :-D

                                  Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                                  My Latest Articles-->** Simplify Code Using NDepend
                                  Basics of Bing Search API using .NET
                                  Microsoft Bing MAP using Javascript

                                  S Offline
                                  S Offline
                                  Suresh Pirsquare
                                  wrote on last edited by
                                  #21

                                  but my id keeps on changing depending on the no. of tabs opened(Firefox). And Each tab has to maintain different ids(customerId for instance). Example I have a gridview having multiple customers. if i open multiple customer details on multiple tabs then there should be different customerid assigned to different pages. In case if i use cookie then it will store a single customerid and when the page makes a request then this id will be assigned to all the pages. I dont want to do like this. I dont think, this is a solution(i.e. Cookie)

                                  Everything Is Possible!

                                  A 1 Reply Last reply
                                  0
                                  • S Suresh Pirsquare

                                    can we store viewstate in masterpage. so that when a user tries to open a new tab, the id is fetched from the viewstate.

                                    Everything Is Possible!

                                    A Offline
                                    A Offline
                                    Aftab Sindhi
                                    wrote on last edited by
                                    #22

                                    Create the public property in Master page. Cast the master page in content page

                                    MasterPage mymasterpage = Page.Master as MyMasterPage;

                                    Use the property in content page to access the customer name.

                                    Response.Write(mymasterpage.CustomerName);

                                    Try it, if it is working

                                    Regards Aftab Sindhi .NET Application Developer U.A.E

                                    S 1 Reply Last reply
                                    0
                                    • S Suresh Pirsquare

                                      but my id keeps on changing depending on the no. of tabs opened(Firefox). And Each tab has to maintain different ids(customerId for instance). Example I have a gridview having multiple customers. if i open multiple customer details on multiple tabs then there should be different customerid assigned to different pages. In case if i use cookie then it will store a single customerid and when the page makes a request then this id will be assigned to all the pages. I dont want to do like this. I dont think, this is a solution(i.e. Cookie)

                                      Everything Is Possible!

                                      A Offline
                                      A Offline
                                      Abhishek Sur
                                      wrote on last edited by
                                      #23

                                      Ok.. As per your requirement is concerned, I think database is the best solution. Even If I was doing this application, I will always use Database. Actually session/viewstate cannot be used to do what you want.. .thats for sure. Other than that, you can use Application object which will reflect to all the session, ... But believe me, from my personal experience I would suggest not to use it, because Server memory is very important, and if your application is using excessive server memory, it may be of high probability of getting the site down any time. So, You have only 2 option, 1. Cookie (Which you dont want) : you can store as many customer ids as you want. As this is stored in client side, performance will not be hampered for the site. 2. Database / XML file in server : I think this is the best way. Its secured and everyone in your situation would have chosen this. Now it depends on you which one you choose. Let me know about it. :rose::rose:

                                      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                                      My Latest Articles-->** Simplify Code Using NDepend
                                      Basics of Bing Search API using .NET
                                      Microsoft Bing MAP using Javascript

                                      S 1 Reply Last reply
                                      0
                                      • A Aftab Sindhi

                                        Create the public property in Master page. Cast the master page in content page

                                        MasterPage mymasterpage = Page.Master as MyMasterPage;

                                        Use the property in content page to access the customer name.

                                        Response.Write(mymasterpage.CustomerName);

                                        Try it, if it is working

                                        Regards Aftab Sindhi .NET Application Developer U.A.E

                                        S Offline
                                        S Offline
                                        Suresh Pirsquare
                                        wrote on last edited by
                                        #24

                                        I dont want to make changes in all the content pages because there are around 40-50 pages.It's tedious to incorporate this changes to all the files. Is there a way by which i have to modify only the master page?

                                        Everything Is Possible!

                                        1 Reply Last reply
                                        0
                                        • A Abhishek Sur

                                          Ok.. As per your requirement is concerned, I think database is the best solution. Even If I was doing this application, I will always use Database. Actually session/viewstate cannot be used to do what you want.. .thats for sure. Other than that, you can use Application object which will reflect to all the session, ... But believe me, from my personal experience I would suggest not to use it, because Server memory is very important, and if your application is using excessive server memory, it may be of high probability of getting the site down any time. So, You have only 2 option, 1. Cookie (Which you dont want) : you can store as many customer ids as you want. As this is stored in client side, performance will not be hampered for the site. 2. Database / XML file in server : I think this is the best way. Its secured and everyone in your situation would have chosen this. Now it depends on you which one you choose. Let me know about it. :rose::rose:

                                          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                                          My Latest Articles-->** Simplify Code Using NDepend
                                          Basics of Bing Search API using .NET
                                          Microsoft Bing MAP using Javascript

                                          S Offline
                                          S Offline
                                          Suresh Pirsquare
                                          wrote on last edited by
                                          #25

                                          I really appreciate your help but i dont think this is a better solution. I would rather create a public property in the masterpage and cast the masterpage in all my content pages(there are around 40-50 pages). although it is a tedious work. I will store the id in the content page's viewstate.

                                          Everything Is Possible!

                                          A 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups