Solved

Refesh and IsPostBack

Posted on 2004-09-09
39
529 Views
Last Modified: 2012-08-13
When you refresh a webpage, this of course shouldn't mean that you are doing a postback because you are reloading the page, but what it seems that my program didn't do the code for if(!PostBack) when I refreshed the page which means that the refresh is a postback......weird

Any help, experts?
0
Comment
Question by:mte01
  • 18
  • 13
  • 4
  • +2
39 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12019312
post your code I think your a little mixed up.

Regards,

Aeros
0
 
LVL 3

Author Comment

by:mte01
ID: 12019378

                                private void Page_Load(object sender, System.EventArgs e)
            {
                  if(!IsPostBack)
                  {
                        lblconn1.Text="";
                        int numrecords = 2;
                        String[] mystr = new String[numrecords];
                        mystr[0] += "DYNAS";
                        mystr[1] += "IDS";
                        String[] strdb = new String[numrecords];
                        strdb[0] += "Dyna_upd";
                        strdb[1] += "IDs_upd";
                        ddlcomp.Items.Clear();
                        for(int i=0;i<numrecords;i++)
                        {
                              mystr[i] += "";
                              ddlcomp.Items.Add(new ListItem(mystr[i],strdb[i]));
                        }
                  }
            }

****************************

When I refresh my webpage it doesn't do the code inside the if block

0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12019407
What that says is If it is a postback, not a refresh do the code, so in your case it should not execute that code, is thats whats happening?
0
 
LVL 3

Author Comment

by:mte01
ID: 12019468
If(!isPostBack)..........evaluates to true (I think) when the page is *not* posted back (i.e. when loaded or re-loaded)
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12019516
my apologies I usally code in vb and did not make the c# equation.  So thats not it.
0
 
LVL 3

Author Comment

by:mte01
ID: 12019553
It's OK, but my concept is still correct: a refresh is not a postback...right??
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12019566
Yes it is
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12019570
try  moving it to the page init event and test
0
 
LVL 3

Author Comment

by:mte01
ID: 12019648
The OnInit method in C# calls the Page_Load event (which runs when a page is loaded or re-loaded)....so it would be the same thing.
0
 
LVL 3

Author Comment

by:mte01
ID: 12019725
Wait...now I seem to see what is happening....let me post the whole code first:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data.SqlClient;

namespace CompanyApp
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class DatabaseChoser : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.DropDownList ddlcomp;
            protected System.Web.UI.WebControls.Button btnselect;
            protected System.Web.UI.WebControls.Label lblconn1;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  if(!IsPostBack)
                  {
                        lblconn1.Text="";
                        int numrecords = 2;
                        String[] mystr = new String[numrecords];
                        mystr[0] += "DYNAS";
                        mystr[1] += "IDS";
                        String[] strdb = new String[numrecords];
                        strdb[0] += "Dyna_upd";
                        strdb[1] += "IDs_upd";
                        ddlcomp.Items.Clear();
                        for(int i=0;i<numrecords;i++)
                        {
                              mystr[i] += "";
                              ddlcomp.Items.Add(new ListItem(mystr[i],strdb[i]));
                        }
                  }
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.btnselect.Click += new System.EventHandler(this.btnselect_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void btnselect_Click(object sender, System.EventArgs e)
            {
                  string connString ="server=localhost; uid=sa; pwd=sa; database="+ ddlcomp.SelectedItem.Value;
                  SqlConnection connection = new SqlConnection(connString);
                  connection.Open();
                  lblconn1.Text = "Connection Established!";
            }

      }
}

******************************************

Refresh works normally if I don't press the select button; however, pressing it is causing the page to reload (of course due to the AutoPostBack), and PostBack would evaluate here to false, and then when I am refreshing the page PostBack is remaining false and this is causing the problem

Any help now??
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12019774
so when you hit the button its not posting is the only problem now?
0
 
LVL 3

Author Comment

by:mte01
ID: 12019879
I am hitting the button (its code is going on normally), the page is reloading and ispostback is false (which is normal), and THEN when I press refresh, the page is re-loading again (of course), and ispostback is *still* false (which is my problem)
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12019917
but if your posting ispostback should be true?  A submit button is a post.  refresh should be false for is post back because nothing is posted
0
 
LVL 3

Author Comment

by:mte01
ID: 12019948
I apologize.....my saying should have been as follows:

I am hitting the button (its code is going on normally), the page is reloading and ispostback is true (which is normal), and THEN when I press refresh, the page is re-loading again (of course), and ispostback is *still* true (which is my problem)
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12019972
oh, so back to square one hugh
0
 
LVL 3

Author Comment

by:mte01
ID: 12019993
Yeh....can you try this code on your PC or something like that??
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12020044
can you post the whole project in a zip or something.
0
 
LVL 3

Author Comment

by:mte01
ID: 12020093
This is the only page of the project right now....

how do I post a zip by the way? can I?
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12020106
You can don't know how, but without your db I'm not gonna be able to see anymore than in here so, just post the entire .aspx page and the .cs here and I'll take a look.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 3

Author Comment

by:mte01
ID: 12020217
>> You can don't know how

I mean how do I post a zip here in EE (I know how to make one)

>> but without your db I'm not gonna be able to see anymore than in here

You can try any database that you have

>> just post the entire .aspx page and the .cs here and I'll take a look

The page that I posted in my 09/09/2004 11:24AM PDT comment is the whole .cs page, and do you want me to post the html of the .aspx file (or upload it which goes back to the problem of not knowing how to upload a file here in EE)
0
 
LVL 3

Author Comment

by:mte01
ID: 12020229
>> but without your db I'm not gonna be able to see anymore than in here

You can try any database that you have or even comment the button_click code
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12020245
I meant I don't know how to post zip on EE
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12020255
try moving this above your page load event:

#region Web Form Designer generated code
          override protected void OnInit(EventArgs e)
          {
               //
               // CODEGEN: This call is required by the ASP.NET Web Form Designer.
               //
               InitializeComponent();
               base.OnInit(e);
          }
         
          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {    
               this.btnselect.Click += new System.EventHandler(this.btnselect_Click);
               this.Load += new System.EventHandler(this.Page_Load);

          }
          #endregion
0
 
LVL 3

Author Comment

by:mte01
ID: 12020459
Didn't work :( (and I also tried your first suggestion on the oninit, and it didn't work either)

You still didn't answer me....can we upload zip files in a question that we are posting on EE?? (I thought that we couldn't)
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12020492
not sure if you can post them on EE!! for the second or third time, you can put it on a webserver somewhere and drop the url
0
 
LVL 3

Author Comment

by:mte01
ID: 12020802
OK...I'll see
0
 
LVL 15

Accepted Solution

by:
Thogek earned 400 total points
ID: 12021008
Wow.  Confusion.  How fun.  :-)

Note that, while a refresh is not a PostBack, you can refresh a PostBack and it'll still be a PostBack.

All a PostBack is is a submission of the server-side form contained within the page.  If you submit a form, and then refresh the resulting page, you are resubmitting the data submitted in the original form to refresh the resulting page.  The server won't know the difference between this refreshed PostBack and the original PostBack, and will treat them both the same as any other PostBack -- with Page.IsPostBack == true.

So, if you refresh a non-PostBack first hit to a page (IsPostBack==false), then the refreshed request also sets IsPostBack=false.  Similarly, if you refresh a PostBack hit to a page (IsPostBack==true), then the refreshed request also sets IsPostBack=true (and re-submits all the same data).
0
 
LVL 3

Author Comment

by:mte01
ID: 12022020
Great! Excellant!! Thogek. Now our problem remains that after submitting a form to the server, how do we reload the original information (as if the page had been first hit)
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12022283
What exactly do you want?
User requests page --> page loads in initial state --> user fills out form --> user submits form --> page reloads in initial state (without user's modifications)?
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12022291
When ..
a page is loaded for the first time = !PostBack OR a page is loaded for the first time + Refresh (using IE refresh button) = !PostBack
all successive loading OR Refresh = PostBack
:))
-@Baan
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12022295
...and the leg bone's connected to the...  ;-)
0
 
LVL 3

Author Comment

by:mte01
ID: 12022324
Specifically:

User requests page --> page loads in initial state --> user fills out form --> user submits form -->(if user presses refresh)
-> page reloads in initial state (User's modifications such as dropdownlist selection, and any label that changed because of what the user selected should go back to initial state, but the connection to the database should remain)

P.S.: If you have another methodology (such as opening a new page or anything like that) please do
0
 
LVL 15

Assisted Solution

by:Thogek
Thogek earned 400 total points
ID: 12022398
You don't really have any control over what sort of request is sent when the user presses Refresh -- unless the user does something odd, the refresh is going to send exactly the same kind of request as was the last.  If s/he hits Refresh after submitting the form, then the ensuing request will re-submit the form data.

Unless your form page redirects to another page after processing the form submission (when PostBack==true).  Such as

    private void Page_Load(object sender, System.EventArgs e)
    {
        if(!IsPostBack)
        {
            // your initial-state loading code (as you posted above) goes here
        }
        else
        {
            // do whatever you're going to do with the submitted data here
           
            Response.Redirect("NewPage.aspx");
        }
    }

NewPage.aspx could be a thank-you page, thanking the user for his/her submission, etc., maybe with a link back to the form page, if the user might be expected to fill it out again.

NewPage.aspx could even be the same form page the user started on (if that isn't considered disorienting to the user to see what looks like his form having been blanked out after s/he just submitted it).  A request resulting from a redirection does not include any form data from a previous submission, and so will be sent and received like a first-hit to the page (i.e., PostBack==false).
0
 
LVL 3

Author Comment

by:mte01
ID: 12022453
Perfect! That's what I needed...thanks for all your time!!
0
 

Expert Comment

by:David Bach
ID: 12835239
This is an interesting post. I have a similar problem as follows which I don't believe the solution here addresses (different problem of course).

My page allows a user to add, edit or delete rows from a data store via a datagrid. When the page is initially presented (postback is false) clicking on the refresh button does not change the page. This is what I expect. Subsequently if the user adds a record to the data store (implemented by a button which initialized and adds a row to the data store and rebinds the datagrid) and clicks the refresh button another record is added to the data store. This would be when postback is true. Each time the refresh button is clicked whatever the last action of the last user request was (add, edit or delete) attempts to repeat. This is disconcerting. If the user needs to refresh the page because not all the page code was received or a graphic didn't load the application will attempt to perform the last update request.

I'm also hearing (please correct me if I heard incorrectly) this same phenomena happens when the back button is clicked.

Any additional information is helpful.


Much thanks ... Dave
0
 
LVL 3

Author Comment

by:mte01
ID: 12835312
When you click the back button, the page is NOT posted back to the server, so your code would not execute.

As for refresh, you are saying that when the user clicks it, his last update is being repeated.....this depends on what code are you putting in the if(isPostBack) block in the Page_Load event.....when you put some code there you will want it to execute when the page is refreshed.....if you don't you should remove it, or do something with it.
0
 

Expert Comment

by:David Bach
ID: 12835423
Hi met01;

The code wired to the datagrid edit, update, cancel and delete buttons for a given row are not invoked from the postback code in the Load procedure.

(shrug) ... Dave
0
 
LVL 3

Author Comment

by:mte01
ID: 12835567
I have a datagrid that has these edit, insert, and update features, and I am not facing this problem when I refresh the page.....post a question with this problem if you want, and I'll try to see how I can help you....
0
 

Expert Comment

by:David Bach
ID: 12835799
Greetings mte01;

Much obliged ... I have reposted my comments here as a new question.

I can easily zip the aspx, vb, database if necessary for further viewing and assistance.


Dave
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now