Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hi,

I am quite new with C#. I have a web app that connects to a MySQL server and binds a gridview to it's data, on the Page_Load method of the code-behind file. I have 3 pages in my web app. Every time I click on the page it always loads the data from the SQL, in short it makes a connection every time I click the page. I want it to load only once, during the first time the page loads, so if the user goes back to the page it won't try to connect to the MySQL server. I currently have this on the Page_Load of each page

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               if(DBconnection=true)
               {
               }
            }
        {


Any input is greatly appreciated.

Thanks!
Posted

Using page property IsPostback helps as long as you are posting back to same page based on various events of the same page.
If you want to persist data across different pages then you need to store it in Session or Cache it. This way, you can get your data back without connecting to database as long as you have data stored.

Following would help:
MSDN: ASP.NET State Management Overview[^]
State management in ASP.NET - 1[^]
 
Share this answer
 
Comments
Manas Bhardwaj 11-Jul-12 16:42pm    
Correct +5!
Sandeep Mewara 13-Jul-12 2:20am    
Thanks Manas.
Hi,

Postback is an event when a form is submitted back to the server for example by a button click.
If you want to fill the grid view only at the first time when the page loads and
if you do not want make a database connection during the button click event or similar events, you must enter the database connection page load and it should be within
if (!IsPostBack)
{
//Your code Here
}

So that the database connection occurs only once.
Not only database connection, you can also enter the code within not postback event for anything that must be loaded only at the beginning like loop statements.

Thank You,

Ganesh
 
Share this answer
 
Comments
Franco Cipriano 11-Jul-12 15:08pm    
Hi,

I currently have that setup, the connection to the DB is within the (!IsPostback) bracket.btw, I have a master page and I navigate through a MenuItem, which points to different MasterPage contents.

Thanks,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900