Button events

R

Rock

I created a button that sets values to dictate how a page is loaded, but
seems the events don't work that way. The page loads first, then the button
click event fires. This seems quite backward, and of course my page doesn't
load with the new information.

For instance, I have a datagrid that displays with information from one
table. If the user clicks on a button, the datagrid changes it's
datasource, so what happens is the original datasource is still bound to the
grid, because it doesn't change until the button is clicked, and since that
event is fired last, well...

How should I be doing this?
 
M

Michael Groeger

Hi,

that's the way a asp.net "lives" look here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconcontrolexecutionlifecycle.asp

I would suggest that you write a method - if not already done - which
displays your datagrid information and do the following:

public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
FillGrid();
}

public void Button_Clicked(object sender, EventArgs e)
{
// change datasource

FillGrid();
}

private void FillGrid()
{
// fill grid
}

Regards,
Michael
 
E

Eliyahu Goldin

With autopostback property set to true, there will be two hidden variable in
your form __EVENTTARGET and __EVENTARGUMENT which you can access in
Page_Load event to get the control name which caused the postback.
__EVENTTARGET will have the object which fired the post back event. Once you
know the sender, you can decide to databind later in the event handler.

Eliyahu
 

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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top