How to control controls postback sequence?

  • Thread starter Thread starter Michael Groeger
  • Start date Start date
M

Michael Groeger

Hi all,

I have designed user controls. One search control where I can search
for items in the database and show them in a grid. This control also
has a button which simply exposes it's click event.

When clicking on the button in this control i want to add the selected
rows in the grid to another grid, which is on my second control. I do
this by handling the Click event of the button in my page and add the
values to arraylist in the session cache. The second control takes
this arraylist and fills its grid with the values from it.

My problem now is, that i cannot determine the sequence of the
postbacks. The event handler for the postback event of my second
control is invoked before the postback event handler of my first
control, so that the selection is not made visible to the client.

How can I control the sequence?

Kind regards,
Michael Groeger
 
All controls will receive the Load event first. Then the postback
events (click in your case) will be fired. Then the PreRender events
will be fired.

The easiest thing for you to do would be to handle the array in the
PreRender event of the control. By the time this is fired all other
control events will have happened
 
All controls will receive the Load event first. Then the postback
events (click in your case) will be fired. Then the PreRender events
will be fired.

The easiest thing for you to do would be to handle the array in the
PreRender event of the control. By the time this is fired all other
control events will have happened
 
Michael Groeger said:
Hi all,

I have designed user controls. One search control where I can search
for items in the database and show them in a grid. This control also
has a button which simply exposes it's click event.

When clicking on the button in this control i want to add the selected
rows in the grid to another grid, which is on my second control. I do
this by handling the Click event of the button in my page and add the
values to arraylist in the session cache. The second control takes
this arraylist and fills its grid with the values from it.

I would recommend that you not do that.

I would recommend that your first control should expose a property of type
ArrayList (or perhaps of IList or ICollection, for generality). I'd have
the Click event of the button in your first control put the values into the
ArrayList property before it raises the Click event of your first control.
When the Page receives the Click event, it can take the ArrayList from the
property in the first control and can put it into the corresponding property
on the second control. You'll have to add the latter.

The general pattern I like is to have the controls notify the Page of any
events from within them, and to allow the Page to read values from the
controls and possibly write values to other controls. That's similar to the
Mediator Pattern.

John Saunders
 
That is a much better suggestion, much more scaleable and reusable,
however more complicated for a beginner to implement as they need to
learn how to create their own events for their custom control. Hence
why I suggested the quick workaround of simply moving the existing code
to PreRender.
Do you specifically think this would not work for any reason?
 
That is a much better suggestion, much more scaleable and reusable,
however more complicated for a beginner to implement as they need to
learn how to create their own events for their custom control. Hence
why I suggested the quick workaround of simply moving the existing code
to PreRender.
Do you specifically think this would not work for any reason?

No, I just think that creating your own controls isn't a beginner work. By
the time you're doing your own controls (even user controls if they have
codebehind), you should be able to create your own events given an example.

Besides, it's nice to set a good example for beginners: they may follow your
example for the rest of their careers!

John Saunders
 
John,

thanks for your reply!

I don't liked the solution I thought of too. I understand your solution but
what I don't like is the way we have to implement things. When implementing
asp.net first time you should rely on that if a control exposes a click
event you probably should take actions there once the control gets clicked.
Now, I have to do things in Load or PreRender where I do not exactly know if
the control really got clicked. I certainly understand that this is an issue
which may happen rather rarely.

But okay, your suggested solution seems to work. Thanks for that ;-)

Michael Groeger
 

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

Back
Top