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. Fundamental ASP.NET Question

Fundamental ASP.NET Question

Scheduled Pinned Locked Moved ASP.NET
csharpquestionasp-netdatabasedesign
7 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.
  • M Offline
    M Offline
    MarkMokris
    wrote on last edited by
    #1

    I have been more of a C# desktop programmer. This is a pretty fundamental ASP.NET question. I realize that all post-backs to a ASP.NET page cause the page to be reloaded. Therefore: 1. Any variables I create global to the page class will be re-initialized with each post-back. Is that correct? 2. Does that mean that nothing really persists from post-back to post-back? 3. What about if I set up a SQLDataSource at design time and bind it to a GridView. Is the SQLDataSource reconnecting and rerunning the SQL with every post-back? Even post-backs that do not involve the GrdiView? Thanks, Mark

    C A N 3 Replies Last reply
    0
    • M MarkMokris

      I have been more of a C# desktop programmer. This is a pretty fundamental ASP.NET question. I realize that all post-backs to a ASP.NET page cause the page to be reloaded. Therefore: 1. Any variables I create global to the page class will be re-initialized with each post-back. Is that correct? 2. Does that mean that nothing really persists from post-back to post-back? 3. What about if I set up a SQLDataSource at design time and bind it to a GridView. Is the SQLDataSource reconnecting and rerunning the SQL with every post-back? Even post-backs that do not involve the GrdiView? Thanks, Mark

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

      1 - yes 2 - yes, unless it's static, or stored in the session, the application or viewstate. 3 - Probably. That's at least in part b/c you're writing bad code, putting your database on your form, instead of in a data layer. .NET does do connection pooling, however.

      Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

      1 Reply Last reply
      0
      • M MarkMokris

        I have been more of a C# desktop programmer. This is a pretty fundamental ASP.NET question. I realize that all post-backs to a ASP.NET page cause the page to be reloaded. Therefore: 1. Any variables I create global to the page class will be re-initialized with each post-back. Is that correct? 2. Does that mean that nothing really persists from post-back to post-back? 3. What about if I set up a SQLDataSource at design time and bind it to a GridView. Is the SQLDataSource reconnecting and rerunning the SQL with every post-back? Even post-backs that do not involve the GrdiView? Thanks, Mark

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

        MarkMokris wrote:

        1. Any variables I create global to the page class will be re-initialized with each post-back. Is that correct?

        Yes.

        MarkMokris wrote:

        2. Does that mean that nothing really persists from post-back to post-back?

        Yes and that depends on how you are maintaining the data during postback.

        MarkMokris wrote:

        What about if I set up a SQLDataSource at design time and bind it to a GridView. Is the SQLDataSource reconnecting and rerunning the SQL with every post-back? Even post-backs that do not involve the GrdiView?

        Yes. You have to very much clear about asp.net page lifecycle and PostBack Property.

        cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

        A 1 Reply Last reply
        0
        • A Abhijit Jana

          MarkMokris wrote:

          1. Any variables I create global to the page class will be re-initialized with each post-back. Is that correct?

          Yes.

          MarkMokris wrote:

          2. Does that mean that nothing really persists from post-back to post-back?

          Yes and that depends on how you are maintaining the data during postback.

          MarkMokris wrote:

          What about if I set up a SQLDataSource at design time and bind it to a GridView. Is the SQLDataSource reconnecting and rerunning the SQL with every post-back? Even post-backs that do not involve the GrdiView?

          Yes. You have to very much clear about asp.net page lifecycle and PostBack Property.

          cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

          A Offline
          A Offline
          AlexeiXX3
          wrote on last edited by
          #4

          Abhijit Jana wrote:

          MarkMokris wrote: What about if I set up a SQLDataSource at design time and bind it to a GridView. Is the SQLDataSource reconnecting and rerunning the SQL with every post-back? Even post-backs that do not involve the GrdiView? Yes. You have to very much clear about asp.net page lifecycle and PostBack Property.

          I tought that: The sql statement wont be executed on every postback, just the first time. On postbacks caused by other controls, the gridview will be regenerated from its view state If the gridview causes a postback, something like sorting or paging, or you call databind, then it will re execute the sql statement again

          Alexei Rodriguez

          N 1 Reply Last reply
          0
          • A AlexeiXX3

            Abhijit Jana wrote:

            MarkMokris wrote: What about if I set up a SQLDataSource at design time and bind it to a GridView. Is the SQLDataSource reconnecting and rerunning the SQL with every post-back? Even post-backs that do not involve the GrdiView? Yes. You have to very much clear about asp.net page lifecycle and PostBack Property.

            I tought that: The sql statement wont be executed on every postback, just the first time. On postbacks caused by other controls, the gridview will be regenerated from its view state If the gridview causes a postback, something like sorting or paging, or you call databind, then it will re execute the sql statement again

            Alexei Rodriguez

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

            Alexei,

            AlexeiXX3 wrote:

            The sql statement wont be executed on every postback, just the first time. On postbacks caused by other controls, the gridview will be regenerated from its view state

            It depends on how you set DataSource to the GridView. If it is set in the markup, like the below

            <asp:GridView ID="grd" runat="server" DataSourceID="sql_ds_id">
            .....
            </asp:GridView>

            GridView will be regenerated on each postback. You can workaround this by removing the DataSourceID="sql_ds_id" from markup and binding from code when required. Then it will be loaded from ViewState each time. Other way is to enable caching on SqlDataSource. :)

            Navaneeth How to use google | Ask smart questions

            A 1 Reply Last reply
            0
            • M MarkMokris

              I have been more of a C# desktop programmer. This is a pretty fundamental ASP.NET question. I realize that all post-backs to a ASP.NET page cause the page to be reloaded. Therefore: 1. Any variables I create global to the page class will be re-initialized with each post-back. Is that correct? 2. Does that mean that nothing really persists from post-back to post-back? 3. What about if I set up a SQLDataSource at design time and bind it to a GridView. Is the SQLDataSource reconnecting and rerunning the SQL with every post-back? Even post-backs that do not involve the GrdiView? Thanks, Mark

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

              MarkMokris wrote:

              Any variables I create global to the page class will be re-initialized with each post-back. Is that correct?

              Yes

              MarkMokris wrote:

              Does that mean that nothing really persists from post-back to post-back?

              As Christian said, static variables will be persisted as static variables are destroyed only when the application domain which hosts ends. ASP.NET provides many other persistent mechanisms. Beginners Introduction to State Management Techniques in ASP.NET[^] will get you started.

              MarkMokris wrote:

              What about if I set up a SQLDataSource at design time and bind it to a GridView. Is the SQLDataSource reconnecting and rerunning the SQL with every post-back? Even post-backs that do not involve the GrdiView?

              Yes if caching is not enabled on SqlDataSource. Using SqlDataSource other than academic purpose is not recommended. It forces you to write SQL inside your markup. There are better ways to get data and do binding. :)

              Navaneeth How to use google | Ask smart questions

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

                Alexei,

                AlexeiXX3 wrote:

                The sql statement wont be executed on every postback, just the first time. On postbacks caused by other controls, the gridview will be regenerated from its view state

                It depends on how you set DataSource to the GridView. If it is set in the markup, like the below

                <asp:GridView ID="grd" runat="server" DataSourceID="sql_ds_id">
                .....
                </asp:GridView>

                GridView will be regenerated on each postback. You can workaround this by removing the DataSourceID="sql_ds_id" from markup and binding from code when required. Then it will be loaded from ViewState each time. Other way is to enable caching on SqlDataSource. :)

                Navaneeth How to use google | Ask smart questions

                A Offline
                A Offline
                AlexeiXX3
                wrote on last edited by
                #7

                N a v a n e e t h wrote:

                GridView will be regenerated on each postback.

                It will be regenerated, but not by executing the sql statement again, it will be regenerated from the viewstate. You can use the sql profiler, or just put a breakpoint in the selecting event of the sqldatasource I have the following code, and it only executes the SQL statement the first time the page is requested but not on postbacks caused a test button on the same page

                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                        DataKeyNames="ID" DataSourceID="SqlDataSource1" 
                        EmptyDataText="There are no data records to display.">
                        <Columns>
                            <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" 
                                SortExpression="ID" />
                            <asp:BoundField DataField="Name" HeaderText="Name" 
                                SortExpression="Name" />
                        </Columns>
                    </asp:GridView>
                

                SqlDataSource1 doesnt have caching enabled

                Alexei Rodriguez

                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