Button click method runs after page loads, which means page doesn't update, how can I solve this?
Asked Answered
R

5

5

I have a button, which updates a value in the database. This value is used to determine what to draw on the page. Because of the page lifecycle though, the page redraws before the button click method is executed, meaning that any changes are not reflected until the page is reloaded again.

What's the best solution for this?

To clarify:

Page has a piece of text, that says "I like cats" if the database value is 1

button 'I hate cats' is pressed, which sets the database value to 0

the page reloads, but still says "I like cats"

the button click event is handled, and database value becomes 0

If the page is refreshed/reloaded, it now correctly says "I hate cats"

It should update when the button is clicked though.

Rumen answered 21/10, 2010 at 11:11 Comment(3)
Hmm..i am not sure what you mean by "page redraws before button click is executed". Is the button click you are referring to a server side event? If so, then it has to be executed before the page's OnRender event...Refer msdn.microsoft.com/en-us/library/ms178472.aspxArleta
Where exactly is the piece of code responsible for displaying "I hate/love cats" ?Chunchung
can you show pageload code please?Heartfelt
S
6

you can use the page prerender event. this fire after the control event in the page lifecycle.

Strasser answered 21/10, 2010 at 12:28 Comment(3)
this should work, but for some reason my eventhandlers don't work when they are set during the pre-render event. However I just realised that I can make them static and show/hide them rather than dynamically creating them... so this should hopefully workRumen
It probably would work but since I am now just showing/hiding controls, I can do it with page_load. Thanks for your help though, it gave me the right answer in the end!Rumen
While I found rauts's link to the lifecycle helpful, I found that the PreRender event was too late. Instead, LoadComplete was the correct event after the control events had fired, but before rendering began.Doubletime
T
5

You can solve your problem by below code, use this end of button_click block

for ASPX:(C#)

Response.Redirect(HttpContext.Current.Request.Path);
Tim answered 27/6, 2012 at 9:19 Comment(1)
Thank you so much. Been struggling with this issue for a couple of days now, and I could never find any proper answer, but yours worked perfectly.Serous
O
0

Where are you querying the database? One easy option would just be to use Response.Redirect back to the page.

I think you could just get the result from the button method and just update a label/literal.

Odaodab answered 21/10, 2010 at 11:27 Comment(0)
S
0

One possible solution is to execute a javascript function that updates the database (using ajax) when the OnClientClick event for the button is raised. In this case, when the page reaches the Page_Load event you will be able to render the appropriate content, as the postback happens after the script is executed. It should work fine if the database update takes a relatively small amount of time.

Semidome answered 21/10, 2010 at 12:19 Comment(0)
M
0

One curiosity, are using any anything like Page.IsPostBack { do something }.. If so, could you check if your update UI code is outside this check.

Modie answered 21/10, 2010 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.