0

In PHP I can define and use a function like this:

<html>
    <head></head>
    <body>
      <?php
      function processItem($item)
      {
           $item = $item * 10;
           ?>
           The item is <strong><?= $item ?></strong><br />
           <?php
      }
      ?>

      <?php
           $items = array(1,2,3,4);
           for($i = 0; $i < $items.length; $i++)
           {
               processItem($items[$i]);
           }
      ?>
    </body>
</html>

If I try to do the same thing in C#, using the <% %> syntax, it will always throw an error. It doesn't like having the extra text/html showing up in the function.

Can someone show me how to write the above example in C#? I am unable to find the terms that will yield a result in Google

2

3 Answers 3

0

Create a *.cshtml file, and use the Razor View Engine. It supports the kind of jumbled syntax you're looking for.

1
  • Can you provide an example? I still don't think I can do this at all from what I've tried and have read
    – zoplonix
    Commented Jun 12, 2013 at 12:56
0

If I'm understanding you correctly, it looks like you want to have your code on the same page as your html. This is very simple to do in asp.net and is sometimes referred to as "Single File" code. Here's an exmaple: http://www.codeproject.com/Articles/8178/Inline-Single-File-vs-CodeBehind

The main difference is that you should put it inbetween the Head tags:

<%@ Page Language="C#" debug="true" trace="false"%>
<html>
  <head>
    <title></title>
    <script runat="server">
      // ASP.NET page code goes here
      void Page_Load() {
        //On Page Load Run this Code
      }
    </script>
  </head>
  <body>    
     <form runat="server">
      <!-- ASP.NET controls go here -->
    </form> 
  </body>
</html>

This wasn't uncommon too long ago, but most people that use asp.net web forms prefer to make use of the file separation and use a code behind file.

If you are coming from PHP and used to that style of coding, then you might find a mix of this and Razor to your liking.

1
  • You are able to the code between those tags in the Page_Load(), but you want to exclude the <% %> tags themselves. Here is a good site to get you started with asp.net programming: asp.net/get-started Commented Jun 12, 2013 at 15:20
0

Solution: thanks to @joel-coehoorn and How to create a function in a cshtml template?

<html>
    <head></head>
    <body>
      @helper processItem(int item)
      {
           item = item * 10;
           <text>
           The item is <strong>@item</strong><br />
           </text>
           var anotherCalculation = item + 2;
           <strong>some more text</strong>
      }

      @{
           var items = myIntArray;
           for(var i = 0; i < items.Count; i++)
           {
               processItem(items[i]);
           }
      }
    </body>
</html>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.