Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code, the c # command, which if statement returns the result true and which returns false, works. But I bet in the jquery commands that if the if condition is not met, the c # command will work

<script type="text/javascript">
    $(document).ready(function () {

        var a = 1;
        if (a == 1)
        {
            alert('1111111111111');
        }

        else
        {           
            alert('2222222');

             @{
             //c# code                 
              Model.Session.SetString("aaaa", "rezajj");
            } 
        }

})
</script>


What I have tried:

Please help me to run the C # command only inside the else{} block
Posted
Updated 21-Nov-20 8:41am
Comments
F-ES Sitecore 21-Nov-20 14:44pm    
c# code runs on the server when the page is being rendered and the results of that rendering are sent to the browser to be processed, at which point your js now runs. So you can't run c# in the browser. The solution depends on what the code does, it might need an ajax call to the server to do some work, or if it needs server-side data you could maybe create js variables with that data in them.

1 solution

You can't. C# code runs entirely and only on the server, never the client.

Javascript code runs only on the client.

The C# code is executed while the page is being generated, before it's sent to the client browser.

You would have to redesign your code with this in mind.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900