This topic is locked
[SOLVED]

 Question about triggering client side code

3/21/2019 1:34:46 PM
ASPRunner.NET General questions
Pete K author

I have a rather unusual situation that I can't seem to wrap my head around. So I have an add page that has server-side code in the Before display event to show/hide certain controls based on a postvalue. In addition to that there is client code in the JavaScript OnLoad event, a function that shows and hides controls on the page based on the user changing the value of a specific drop-down control. Finally, I have added some advanced server-side validation code in the Before record added event. If the validation fails, I display an error message using the built-in message variable, and return false.
Everything works fine except for the case where my custom validation code fails. In that case, even though the page appears to reload, neither the Before display event nor the JavaScript code executes and the control visibility reverts to their default (design-time) states.
So my question is, is there a way to make all of this work when a record is not added, that is, control the display of the controls as if the page were loading for the first time when an error situation is encountered?

jadachDevClub member 3/21/2019

Can you put a reload or redirect to add page if an error occurs? You could display the error message via a session variable so not to confuse the user.

Pete K author 3/22/2019



Can you put a reload or redirect to add page if an error occurs? You could display the error message via a session variable so not to confuse the user.



Yeah, I could do that Jerry. I was hoping that wouldn't be necessary, as I have a lot of fields which means I'll have to save them all to session variables and then add logic to re-populate them upon re-load, but only in this case. Thanks for the suggestion.

admin 3/22/2019

This is definitely not happening in my test ASPRunner.NET 10.1 project.
http://demo.asprunner.net/volinrok_yahoo_com/Project17v101/categories/add

BeforeDisplay event prints "yes" in the top left corner and Javascript OnLoad event hides Description control. Just enter category name and click Save.

Pete K author 3/23/2019



This is definitely not happening in my test ASPRunner.NET 10.1 project.
http://demo.asprunne.../categories/add

BeforeDisplay event prints "yes" in the top left corner and Javascript onload event hides Description control. Just enter category name and click Save.


Very odd, Sergey. On my phone the description control remains visible after the postback.


admin 3/23/2019

Pete,
on your screenshot the control is hidden. What you see is a label which wasn't meant to be hidden. The point is that both server and client events are executed.

Pete K author 3/25/2019



Pete,
on your screenshot the control is hidden. What you see is a label which wasn't meant to be hidden. The point is that both server and client events are executed.


Thank you for the clarification, Sergey. It's reassuring to know that this works as I expect it to. So now I can focus on what's going in in this specific case. I guess the only viable possibility is that I've broken something with my code. I'll keep searching.

admin 3/25/2019

Just to make sure that no other code gets in the way start by removing all code from BeforeAdd event but "return false;" and see what happens.

Pete K author 3/26/2019

I figured it out. In the case of the server code, my code was dependent on a URL querystring which was not getting repeated upon the postback. I solved that by changing it to a session variable. In the case of the client code, I foolishly forgot that the state of my show/hide code was dependent on the OnChange event being fired by the changing of a control. I updated the JavaScript to fire the OnChange event upon loading if it is a postback (submit=1).
Thanks for the help and suggestions. Once again I assumed ASPRunner.NET was at fault when it was actually the guy at the keyboard.