javascript - Avoid Execution of Embedded Code Blocks in ASP.NET -
in webforms project, on particular page, have javascript method in aspx file. inside use 2 embedded code blocks.
<script type="text/javascript"> function showmessage(idactitygroup) { var r = confirm("message"); if (r == true) { //first code block <%string idactitygroup = "' + idactitygroup + '"; //second code block '<%session["waitlist"] = "yes"; %>'; //last javascript instruction window.location.href = '/register'; } </script>
the problem these 2 lines executed each time click event of button triggered. when in event of codebehind button has done work, enters aspx , executes 2 lines. , want them run when function called. method inside updatepanel. have tried javascript function out of updatepanel same thing still happening.
is there way avoid behavior? thank much.
before fixing issue, should understand happening , why 2 lines executed every time press button. should first review this document page lifecycle in asp.net
just after prerender stage, inline code (the 2 lines talking about) executed , html generated.
when click button, submits postback , asp.net re-render page, hence 2 lines executed second time.
now, don't think can achieve want way. i.e. second code block:
<%session["waitlist"] = "yes"; %>
would assigns "yes" 'waitlist' key in session state every time page loads. should instead try generate postback handle on code behind.
<script type="text/javascript"> function showmessage(idactitygroup) { var r = confirm("message"); if (r == true) { //first code block __dopostback('handlemessageconfirmed',null) } </script>
Comments
Post a Comment