Fundamental ASP.NET Question
-
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
-
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
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 )
-
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
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
-
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
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
-
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
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 theGridView
. 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 fromViewState
each time. Other way is to enable caching onSqlDataSource
. :)Navaneeth How to use google | Ask smart questions
-
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
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
. UsingSqlDataSource
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
-
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 theGridView
. 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 fromViewState
each time. Other way is to enable caching onSqlDataSource
. :)Navaneeth How to use google | Ask smart questions
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