0

I have two radio button on my asp.net page, with AutoPostBack = True
When I click on either of those, they each set a flag SaveData=False

However, when I click on them, the page_load event occurs first, so the page_load event saves the data, then the radiobutton_OnCheckedChanged event is triggered.

How can I trigger the OnCheckedChanged before the page_load event?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Troy
  • 1,659
  • 4
  • 19
  • 33
  • Its impossible to implement. But for flag setting you can use viewstate or session to set value in onCheckedChanged event and use it where you want. – Emaad Ali Oct 07 '11 at 19:15

5 Answers5

2

You can't.

You need to read up on the page lifecycle.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

You can't trigger the OnCheckedChanged before radiobutton_OnCheckedChanged because that's the normal page life cycle.

You can check in Page_Load if Page.IsPostBack is true or not, and then don't save (or save) the data.

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Well, there is a button that is supposed to save the data, so the if not ispostback theory wont work. – Troy Oct 07 '11 at 19:30
  • 1
    @Troy, it will work; whether this is the best way to do it is a separate discussion. The bottom line is that ASP.NET is not WinForms. The Page_Load event will always fire before the OnCheckChanged on your radio button. You can prevent the saving of the data by checking IsPostBack and whether radioButton.Checked==true. – Icarus Oct 07 '11 at 20:09
1

Troy - you will have to take care of this on the client side (javascript / jquery). Your AutoPostBack = True causes the form to do a postback which calls page load. Its all about how the page lifecycle and server side events work.

JonH
  • 32,732
  • 12
  • 87
  • 145
0

You can't the page always needs to load before events can be fired. This has to do with control creation, view state management, control state, etc.

What you want to do in your Page_Load is something like:

if(!this.IsPostBack) 
{
   // Do the stuff you want on the initial page load
}

Anything that you do not want to happen when the radio buttons are clicked, put it inside the if {} block. IsPostBack indicates that the page is processing a post-back event, which is what happens when you click one of your radio buttons.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
0

You can't change the order/sequence of event handler execution. However you may move the code inside the page_load to another event handler.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186