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. How dynamic objects work during a post back

How dynamic objects work during a post back

Scheduled Pinned Locked Moved ASP.NET
sysadminhelpquestion
11 Posts 6 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.
  • C Offline
    C Offline
    compninja25
    wrote on last edited by
    #1

    Hello everyone, I'm working on a web form and I'm dynamically creating a table on the page with two columns. One column has the text description, and the second has a check box which the user can check or uncheck to indicate that they want that particular item to activated. Because each user will have a different amount of options, I felt it best to build the table rows on the fly after I discover who the user is. The only problem I am fighting with now is that after I build the page and do any post back, the dynamic control is no longer there. Has anyone out there ever tried anything similar? I'm thinking I need to modify the post back somehow to indicate that I want the control to submit the check boxes back to the server so I can process them. Thanks!

    Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

    C 1 Reply Last reply
    0
    • C compninja25

      Hello everyone, I'm working on a web form and I'm dynamically creating a table on the page with two columns. One column has the text description, and the second has a check box which the user can check or uncheck to indicate that they want that particular item to activated. Because each user will have a different amount of options, I felt it best to build the table rows on the fly after I discover who the user is. The only problem I am fighting with now is that after I build the page and do any post back, the dynamic control is no longer there. Has anyone out there ever tried anything similar? I'm thinking I need to modify the post back somehow to indicate that I want the control to submit the check boxes back to the server so I can process them. Thanks!

      Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You need to create the control on every postback. And you need to do it before pageload if you want any sort of viewstate to be restored.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      C 1 Reply Last reply
      0
      • C Christian Graus

        You need to create the control on every postback. And you need to do it before pageload if you want any sort of viewstate to be restored.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        C Offline
        C Offline
        compninja25
        wrote on last edited by
        #3

        Hi Christian, Thanks for the input. Kudos on your blog too! So, I need to use the Page.PreLoad event and build my control there? Right now I'm building the table using data from my database, so if I did that during the page preload, wouldn't that overwrite the post back arguments? Or do I have that backwards? If I create the table during the preload, will the changes in the post back overwrite the results from the database? Sorry if it sounds like a 'you should already know this' type question. I've been reviewing the articles I've found online about the page lifecycle, but it still seems a little vauge to me.

        Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

        N C 2 Replies Last reply
        0
        • C compninja25

          Hi Christian, Thanks for the input. Kudos on your blog too! So, I need to use the Page.PreLoad event and build my control there? Right now I'm building the table using data from my database, so if I did that during the page preload, wouldn't that overwrite the post back arguments? Or do I have that backwards? If I create the table during the preload, will the changes in the post back overwrite the results from the database? Sorry if it sounds like a 'you should already know this' type question. I've been reviewing the articles I've found online about the page lifecycle, but it still seems a little vauge to me.

          Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

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

          compninja25 wrote:

          So, I need to use the Page.PreLoad event and build my control there?

          Init will be a good place for dynamic controls. It looks like you are displaying tabular data. Why don't you try controls like GridView or DataGrid? GridView has inbuilt checkbox support. In DataGrid, a checkbox can be added by using a template column. If you can do that, you don't need to worry about the viewstate issues. :)

          Navaneeth How to use google | Ask smart questions

          C 1 Reply Last reply
          0
          • C compninja25

            Hi Christian, Thanks for the input. Kudos on your blog too! So, I need to use the Page.PreLoad event and build my control there? Right now I'm building the table using data from my database, so if I did that during the page preload, wouldn't that overwrite the post back arguments? Or do I have that backwards? If I create the table during the preload, will the changes in the post back overwrite the results from the database? Sorry if it sounds like a 'you should already know this' type question. I've been reviewing the articles I've found online about the page lifecycle, but it still seems a little vauge to me.

            Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            compninja25 wrote:

            So, I need to use the Page.PreLoad event and build my control there?

            I believe that is correct.

            compninja25 wrote:

            Right now I'm building the table using data from my database, so if I did that during the page preload, wouldn't that overwrite the post back arguments?

            you do it precisely so that you can get your postback data. The control needs to exist in order for the system to populate it with data from postback. I'd expect if you want to put your own data in there, you'd do it in prerender, which is always the right place. But for viewstate to work, it needs to be created before page load.

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            A 1 Reply Last reply
            0
            • C Christian Graus

              compninja25 wrote:

              So, I need to use the Page.PreLoad event and build my control there?

              I believe that is correct.

              compninja25 wrote:

              Right now I'm building the table using data from my database, so if I did that during the page preload, wouldn't that overwrite the post back arguments?

              you do it precisely so that you can get your postback data. The control needs to exist in order for the system to populate it with data from postback. I'd expect if you want to put your own data in there, you'd do it in prerender, which is always the right place. But for viewstate to work, it needs to be created before page load.

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

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

              Christian Graus wrote:

              compninja25 wrote: So, I need to use the Page.PreLoad event and build my control there? I believe that is correct.

              Boss, I have one small doubt over here, AFAIK , we need to create the control either Init or PreInit for maintaining the post back. Because Though, LoadViewState and Loadpostback Data Calls before Page_Load(), ASP.NET Creates a control hierarchy only at Init event, based on that the postback data and view state data maintains. So, How it would restore view state and postback data if we create control on Pre_Load() ?.

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

              A 1 Reply Last reply
              0
              • A Abhijit Jana

                Christian Graus wrote:

                compninja25 wrote: So, I need to use the Page.PreLoad event and build my control there? I believe that is correct.

                Boss, I have one small doubt over here, AFAIK , we need to create the control either Init or PreInit for maintaining the post back. Because Though, LoadViewState and Loadpostback Data Calls before Page_Load(), ASP.NET Creates a control hierarchy only at Init event, based on that the postback data and view state data maintains. So, How it would restore view state and postback data if we create control on Pre_Load() ?.

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

                A Offline
                A Offline
                Arun Jacob
                wrote on last edited by
                #7

                Abhijit Jana wrote:

                we need to create the control either Init or PreInit for maintaining the post back

                I guess whatever u said is correct its better to create dynamic controls on Init according to the Event Cycle.Control developers are mostly rely on this event.Otherwise we need to manually keep the View state. :)

                Arun Jacob http://codepronet.blogspot.com/

                A 1 Reply Last reply
                0
                • A Arun Jacob

                  Abhijit Jana wrote:

                  we need to create the control either Init or PreInit for maintaining the post back

                  I guess whatever u said is correct its better to create dynamic controls on Init according to the Event Cycle.Control developers are mostly rely on this event.Otherwise we need to manually keep the View state. :)

                  Arun Jacob http://codepronet.blogspot.com/

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

                  Yes, But my question was different. We need to create those dynamic control over Init and Preinit() because of maintaining view state and load postback data. And In Init Event ASP.NET Create a control list to do all the operation during page life cycle. Suppose you are creating one Dynamic Textbox on Page_Load and done some post back after changing some value of that textbox .As per the Page_Life Cycle goes, View State Data for the newly created text box has been saved because, SaveViewdata() calls after Pre_Render and Before Render(). Its means My View State data has been saved but, now why that data would not restored ? Though my ViewState data has already saved and LoadView State Data is calling before Page_load. So it should restore the data. But It should not. Beacuse, the reason I have said. In Init() asp.net creats an Control Tree. If you create the control on/after Page_Load, they are not a member of that tree. There is also some workaround for it. ;) ;)

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

                  R C 2 Replies Last reply
                  0
                  • A Abhijit Jana

                    Yes, But my question was different. We need to create those dynamic control over Init and Preinit() because of maintaining view state and load postback data. And In Init Event ASP.NET Create a control list to do all the operation during page life cycle. Suppose you are creating one Dynamic Textbox on Page_Load and done some post back after changing some value of that textbox .As per the Page_Life Cycle goes, View State Data for the newly created text box has been saved because, SaveViewdata() calls after Pre_Render and Before Render(). Its means My View State data has been saved but, now why that data would not restored ? Though my ViewState data has already saved and LoadView State Data is calling before Page_load. So it should restore the data. But It should not. Beacuse, the reason I have said. In Init() asp.net creats an Control Tree. If you create the control on/after Page_Load, they are not a member of that tree. There is also some workaround for it. ;) ;)

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

                    R Offline
                    R Offline
                    r a m e s h
                    wrote on last edited by
                    #9

                    What if I need to create a GridView with dynamic template columns based on the user preference or search conditions? The controls values cannot be accessed in Page_Init event. Therefore you cannot create dynamic controls in Page_Init event based on the values entered in controls such as Textboxe. Also in Page_Init, if the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state. But during load, if the current request is a postback, control properties are loaded with information recovered from view state and control state. Therefore, Page Load is also good place to create dynamic controls.

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

                      compninja25 wrote:

                      So, I need to use the Page.PreLoad event and build my control there?

                      Init will be a good place for dynamic controls. It looks like you are displaying tabular data. Why don't you try controls like GridView or DataGrid? GridView has inbuilt checkbox support. In DataGrid, a checkbox can be added by using a template column. If you can do that, you don't need to worry about the viewstate issues. :)

                      Navaneeth How to use google | Ask smart questions

                      C Offline
                      C Offline
                      compninja25
                      wrote on last edited by
                      #10

                      Hi Navaneeth, Indeed, it is tabular data. I've done what you are suggesting before with much larger amounts of data, but because in general the task at hand is only calling for 2 through 5 options to display to the user, I originally thought it would be quicker to just build a quick table and add the few rows. Anyhoo, I did consider using the data grid, but figured I'd like to look into trying to get the dynamic control working first as a way to learn more about the page life cycle and view state. Hopefully it will come in handy somewhere down the line! :) Thanks again for your response!

                      Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

                      1 Reply Last reply
                      0
                      • A Abhijit Jana

                        Yes, But my question was different. We need to create those dynamic control over Init and Preinit() because of maintaining view state and load postback data. And In Init Event ASP.NET Create a control list to do all the operation during page life cycle. Suppose you are creating one Dynamic Textbox on Page_Load and done some post back after changing some value of that textbox .As per the Page_Life Cycle goes, View State Data for the newly created text box has been saved because, SaveViewdata() calls after Pre_Render and Before Render(). Its means My View State data has been saved but, now why that data would not restored ? Though my ViewState data has already saved and LoadView State Data is calling before Page_load. So it should restore the data. But It should not. Beacuse, the reason I have said. In Init() asp.net creats an Control Tree. If you create the control on/after Page_Load, they are not a member of that tree. There is also some workaround for it. ;) ;)

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

                        C Offline
                        C Offline
                        compninja25
                        wrote on last edited by
                        #11

                        Hello abhijit, Thank you for your input. Do you have any suggestions regarding tutorials or white papers on the page life cycle? I've read through this one click here , but the whole concept is still quite foreign to me. I believe that's why I'm running into this problem; because I cannot yet visualize the 'flow' of the page. Thanks!

                        Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

                        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