General misunderstanding of page events

A

AC

I've got a page which, when it initially loads (i.e. IsPostBack ==
false), loads all my application's users into one list and all the
roles into another. If it is a postback, it loads details of the
selected user into a bunch of literals, textboxes, etc. So far, so
good.

Generally, it goes like this:

Page_Load
{
if (IsPostBack)
// load user UserList.SelectedValue
else
{
// populate UserList and RoleList
}
}

Then I added an "Apply changes" button and handled the "if something
changed update user" stuff in the button's _Click event.

Button_Click
{
// if something changed, update user
}

However, the _Click event occurs after the Page_Load event, so before
all the "if something changed" code gets executed, all the fields are
set back to their original pre-changed values.

Clearly, I'm still in Windows Forms mode and haven't quite got to
grips with web forms... can anyone shed a bit of light please? I
think it's mostly just a case of reorganising but it feels like I
might've missed a trick...

Thanks for your help!
 
M

Mark Rae

AC said:
No that isn't quite it - the problem is that I'm reloading the
controls with (the old) values from the database in Page_Load before I
update the database with the new values in Button_Click. It's more of
a chicken/egg problem. Do you see? Or am I not explaining the
problem properly???

Oh right - so you don't want to fetch the existing values from the database
when the page is posted back...?

Page_Load
{
if (!IsPostBack)
{
BindData();
}
}

Button_Click
{
// save the data to the database
BindData(); // you may not need / want to do this
}

private void BindData()
{
// fetch the data from the database and bind to controls
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top