<script runat="server"> vs <% %>

Corey picture Corey · Feb 18, 2015 · Viewed 8.6k times · Source

I'm here learning ASP.NET which is why I'm obviously here asking a question. So when I run a statement inside of <% %> delimiters everything works fine. I try to instead run it in a <script runat="server"> and it doesn't work. I'm just curious to what is so significant different between the two methods. I am suppose to be using the script method, but it only works with the <% %>.

My example is this... basic standard form getting "userInput" (a number) from the POST method.

        <form action="calcprime.aspx" method="post">
            Enter a number between 1-999: 
            <input type="text" name="userInput" maxlength="3" />
            <input type="submit" name="submit" value="Submit" />
        </form>

Then if I need to convert the string into an integer to do math on it I would do this...

<%@ Page Language="C#" %>

<script runat="server">
    int num = int.Parse(Request.Form["userInput"]);
</script>

<%=num%>    // <-should display the number in theory..(in my head)

Unfortunately the above code errors and does not work for me, However the alternative using only <% %> using the same exact method of code works 100% fine. Such as below...

<%@ Page Language="C#" %>

<%
    int num = int.Parse(Request.Form["userInput"]);
    Response.Write(num);    //displays the number as it should.. 100% working
%>

So my question is. Why doesn't the script way work? Isn't it the same thing? What is the correct way to handle this basic scenario in ASP.NET using C#? Is my approach even practical, or is there a standard that I should be aware of? The number will have math done to it, which is why I need it converted to an integer. This is just a somewhat basic foundational stuff here that I feel I should know the correct way to go about it before learning bad practices.

Answer

mason picture mason · Feb 18, 2015
<script runat="server">
    int num = 1;
</script>

<%=num%>

That works fine on my machine. I see the 1 on my page.

However, this doesn't work:

<script runat="server">
    int num = int.Parse(Request.Form["userInput"]);
</script>

<%=num%>  

I get this error:

CS0120: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Request.get'

I suspect you got that error too, but didn't include it in your question. NOTE if you get an error in your code, include it in your question. Don't assume we know you got an error.

This works, assuming I add query string appropriately to the request URL:

<script runat="server">
    int num = int.Parse(HttpContext.Current.Request.QueryString["randomnum"].ToString());
</script>

<%=num%>  

I suspect this will work too, assuming you've already posted form values to the page. However, your question was not complete, so I don't know if you did it or not. That just goes to show, you need to submit a Minimal, Verifiable, and Complete example.

<script runat="server">
    int num = int.Parse(HttpContext.Current.Request.Form["userInput"]);
</script>

<%=num%>

In the end however, you probably shouldn't be doing code blocks inline (whether using a script tag or inline expression) on your page. That's better handled on code behind. <% %> stuff is fine in some contexts, but you should usually only use it to inject a value onto the page, for example when you're eval'ing something in a repeater. And if you find yourself doing a lot of those inline expressions, you might consider switching to ASP.NET MVC or Web Pages. Both of those use the Razor view engine, which is much cleaner.