How to prevent postback that happens before the click event in a server control

I’ve got an aspx page with two ascx controls. The first control is for adding new data, the second control is for displaying that data.

<div>
   <cc:EntryControl ID="MyEntryControl" runat="server" />
   <div>other stuff</div>
   <cc:ShowDataControl ID="ShowMyData" runat="server" />
</div>

There’s a button click event in the EntryControl codebehind:

protected void btnAddData_Click(object sender, EventArgs e) 
{
     //add data to db
}

The problem is that the button to add the data causes a postback, which first loads the page and the two controls, THEN fires the button click event, which leaves me with the existing, previously added data, but not the newly added data with that button click. The data gets added but it just doesn’t show b/c the postback for the ShowDataControl has already happened.

What do I need to do so that the newly added data will show?

  • Are you doing something in Page_Load (or any other lifecycle event that occurs before the Click event handler) which changes the state of the page and removes the data you’re looking for? If so then, well, don’t do that. Are you just looking for the IsPostBack property to conditionally perform logic in Page_Load?

    – 

  • @David – not that I know of – but how would I check to make sure?

    – 

  • I guess you’d have to do some debugging to narrow down the problem. The fact that a button causes a postback isn’t a problem (as indicated in the question) because that’s exactly what server-side button controls do. What you’ll need to find is where/how the “previously added data” is added to the page at all and confirm where/how that logic is being invoked on a postback. The code shown in the question simply doesn’t indicate anything about the problem.

    – 

  • The controls are posting back first, before the click event gets fired. I’m not even sure what code would illustrate that problem. Why does the click event happen after the postback?

    – 

  • 2

    It sounds like you’re misunderstanding how WebForms events, WebForms page lifecycle, or even HTTP in general works. The postback is the request being made to the server. No server-side code is going to respond to a client-side event (such as a click) before the server receives the request being made from the client. When WebForms receives a request, it executes its standard page lifecycle. If what you’re observing is that Page_Load (or similar, such as Page_Init) executes before the Click function then that is expected and documented behavior.

    – 

As noted in the comments here, a simple button click will:

post-back the page.

Page load event will run.

Then your button code stub will run.

What this means is that EACH and EVERY time you use standard button, the page post-back will occur, and the page load event will run first, and run EVERY time for any button click (which is a post-back).

What this means is that 99 out of 100 web pages in NEAR EVERY case will require a !IsPostBack code stub for the REAL first page load.

A good example is simple code that loads up a dropdown list.

If on every button click re-load the drop-down list (combo box), then of course what the user selected will be lost if I re-load the combo box each time.

So, a drop-down list like this on the page:

        <h3>Select Hotel to Edit</h3>

        <asp:DropDownList ID="cboHotels" runat="server"
            DataValueField="ID"
            DataTextField="HotelName"
            OnSelectedIndexChanged="cboHotels_SelectedIndexChanged"
            width="240px"
            AutoPostBack="true"
            >
        </asp:DropDownList>

Now, in page load event, then I have this code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    void LoadData()
    {
        string strSQL =
            @"SELECT * FROM tblHotelsA ORDER BY HotelName";

        cboHotels.DataSource = General.MyRst(strSQL);
        cboHotels.DataBind();
        cboHotels.Items.Insert(0, "Select Hotel");

    }

So, note how I wrapped the setup code, the data loading code INSIDE of the !IsPostBack code stub.

Hence, additional button clicks, or additional post-backs of the page WILL NOT matter, since with the !IsPostBack code stub, that setup and loading of data code will ONLY run the first time and on the first true real page load.

Since the setup and data loading code is inside of the !IsPostBack stub, then additional button clicks and post-backs don’t matter, and more important do NOT overwrite the combo box settings, since a re-load and re-binding of the combo box each time would of course cause loss of the current selected value in that control.

So, while it is fine and common to use the page load event to load up data, setup controls with some default values etc.?

Well, keep in mind that your software has to be written with the fact that page load fires each and every time on a post-back and runs BEFORE your code stub for the given control event runs.

Hence, with the above it does not matter that page load will run many times, and run just about all the time, since our data loading and setup code only runs ON THE FIRST real page load, and does so due to having the !IsPostBack code stub.

Needless to say, I would say that for the last 200+ web pages I have created, quite sure about 199 of them had the !IsPostBack code stub, and in fact you really can’t build or design a working webforms page without this !IsPostBack code stub in the page load event.

Leave a Comment