Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 542
  • Last Modified:

Refesh and IsPostBack

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
mte01
Asked:
mte01
  • 18
  • 13
  • 4
  • +2
2 Solutions
 
AerosSagaCommented:
post your code I think your a little mixed up.

Regards,

Aeros
0
 
mte01Author Commented:

                                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
 
AerosSagaCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mte01Author Commented:
If(!isPostBack)..........evaluates to true (I think) when the page is *not* posted back (i.e. when loaded or re-loaded)
0
 
AerosSagaCommented:
my apologies I usally code in vb and did not make the c# equation.  So thats not it.
0
 
mte01Author Commented:
It's OK, but my concept is still correct: a refresh is not a postback...right??
0
 
AerosSagaCommented:
Yes it is
0
 
AerosSagaCommented:
try  moving it to the page init event and test
0
 
mte01Author Commented:
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
 
mte01Author Commented:
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
 
AerosSagaCommented:
so when you hit the button its not posting is the only problem now?
0
 
mte01Author Commented:
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
 
AerosSagaCommented:
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
 
mte01Author Commented:
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
 
AerosSagaCommented:
oh, so back to square one hugh
0
 
mte01Author Commented:
Yeh....can you try this code on your PC or something like that??
0
 
AerosSagaCommented:
can you post the whole project in a zip or something.
0
 
mte01Author Commented:
This is the only page of the project right now....

how do I post a zip by the way? can I?
0
 
AerosSagaCommented:
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
 
mte01Author Commented:
>> 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
 
mte01Author Commented:
>> 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
 
AerosSagaCommented:
I meant I don't know how to post zip on EE
0
 
AerosSagaCommented:
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
 
mte01Author Commented:
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
 
AerosSagaCommented:
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
 
mte01Author Commented:
OK...I'll see
0
 
ThogekCommented:
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
 
mte01Author Commented:
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
 
ThogekCommented:
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
 
DotNetLover_BaanCommented:
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
 
ThogekCommented:
...and the leg bone's connected to the...  ;-)
0
 
mte01Author Commented:
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
 
ThogekCommented:
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
 
mte01Author Commented:
Perfect! That's what I needed...thanks for all your time!!
0
 
David BachCommented:
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
 
mte01Author Commented:
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
 
David BachCommented:
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
 
mte01Author Commented:
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
 
David BachCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 18
  • 13
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now