locked
how to communicate with Razor and HTML RRS feed

  • Question

  • User616014865 posted

    In ASP.Net,  I frequently used two methods

    1) from ASP to CS : _doPostBack( ) to raise a button event in the CS page

    2) from CS to ASP:  ClientScript.ScriptBlock( ) to display message in the client

    What are equivalent Razor ways to achieve (1) and (2)?   I mean

    1) from HTML button to execute a part of @{..} block.   

    2) from @{..} block to execute a javascript function or affect HTML hide/show

    Monday, May 4, 2015 12:10 PM

Answers

  • User281315223 posted

    So there are some things to consider here and they primarily revolve around how ASP.NET Web Pages (i.e. Razor). Basically, you need to think of it as a traditional web request/response environment that uses GET and POST requests.

    When a page is originally requested, a GET request is performed that initially loads the content of the page. However, when the <form> of that page is submitted (via a <input type='submit' /> button) then a POST request will be sent to the server. A POST request is going to be what you would associate a PostBack to in ASP.NET Web Forms.

    1) from ASP to CS : _doPostBack( ) to raise a button event in the CS page

    Essentially what you would want to do would be to create a <input type='submit' /> button within your page and clicking this would essentially perform a PostBack. You could then apply some server-side code within your page to check if a PostBack occurred using the IsPost property :

    @{
        // If a PostBack occurred, add your button click logic here
        if(IsPost) {
            // Do something here
        }
    }   

    2) from CS to ASP:  ClientScript.ScriptBlock( ) to display message in the client

    You could try using something like this :

    @if(IsPost){
       <text>
            <script type='text/javascript'>
                 alert("Your Script Here");
            </script>
       </text>
    }

    This is just an example, but it could give you an idea of how to perform certain script-related code. Likewise, you could perform the same basic logic to show and hide certain elements :

    <div id='YourElement' style='@(!IsPostBack ? "display:none" : "")'>
        Example!
    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 4, 2015 5:15 PM