Jay
Jay

Reputation: 27492

reload current page in asp.net

I have a session variable that changes some things about how a page looks. I have a button that changes the value of this session variable. But ... the onClick event happens after page load, so by the time I update the session variable based on the button click, it's too late, the page has already been loaded.

Theoretically I could put all the logic about changing the display into a function and call it from page load, and then call it again from the onclick after the variable as been updated. But this is impractical: there are many user controls that check the value, used on many different pages in different combinations. I would have to hard-code the list of user controls on each page, and if someone added a new user control to a particular page, they'd have to remember to update this function, which is lame.

Is there a way to force a page reload? (I can use response.redirect back to myself and it works. If all else fails I guess this is what I'll do. But it means an extra round trip to the server, which is clumsy.)

Is there a way to process the onclick before the page load?

Some other magic solution?

Upvotes: 0

Views: 6517

Answers (4)

ChrisLively
ChrisLively

Reputation: 88072

A design with a layout predicated on the existence of a session variable which won't exist until after it's been render is a huge design error. I like to call it the "Chicken or the Egg" syndrome. (yes, you can quote me.. ;)

I'd argue that your controls shouldn't get their layout completed in the on render. Instead, use a method (similar to databinding) where you can "rebind" the controls with the new session value on demand. This method would show/hide things based on the updated values.

Upvotes: 0

The Evil Greebo
The Evil Greebo

Reputation: 7138

If you have to change the look and feel of a page based on a specific value which can change, then you should have dedicated functions that set up the look and feel in a single unified place, and then you call those functions in every case where a value that affects the look and feel is called.

Examples:

private void SetDivVisibility()
{
  // display logic here based on variables

}

private void MyControl_Click(...)
{
   myvalue = blah;
   SetDivVisibility();
}

It helps to bear in mind that the actual rendering of the page is last thing that happens, after both page load AND event processing.

Upvotes: 3

Mhmd
Mhmd

Reputation: 5167

you may put your code of styling your page in a void called by the page_load normally and called again from buttonclick

or call response.redirect to same url

or even onClick is client side use window.location.href

Upvotes: 0

Oded
Oded

Reputation: 499202

Theoretically I could put all the logic about changing the display into a function and call it from page load

That's how you should do it. Cleanup your logic and markup - refactor and keep it DRY. That should help.

I can use response.redirect back to myself

That's the other option. Yes, a round trip is nasty.

Upvotes: 2

Related Questions