问题
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?
回答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.
回答2:
You can't.
You need to read up on the page lifecycle.
回答3:
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.
回答4:
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.
回答5:
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
.
来源:https://stackoverflow.com/questions/7691833/in-asp-net-page-load-occuring-before-radio-button-oncheckedchanged-event