tags:

views:

120

answers:

2

I'm playing around with inline code blocks in asp.net. Can somebody tell me why the following code doesn't work?

<%@ Language="C#" %>
<%
    Response.Write(TestClass.ShowMessage());

    public class TestClass
    {
     public static string ShowMessage()
     {
      return "This worked!!";
     }
    }
%>

I get the following error message: CS1513: } expected

+2  A: 

I think to be able to write a whole class you need to place it inside a tag block

<script runat="server" language="C#">
//Put your class here
</script>
bashmohandes
+3  A: 

You've got the Response.Write floating there outside of a function.

Why are you not placing this code in a Script tag:-

<script runat="Server">
public class TestClass
{
    public static string ShowMessage()
    {
            return "This worked!!";
    }
}

</script>

Then:-

<%=TestClass.ShowMessage()%>

Note the <%=expr %> is handle specially.

Many consider this to be an impure approach. You could do this:-

<script runat="Server">
protected void Page_Load(object sender, EventArgs e)
{
        litMessage = TestClass.ShowMessage();
    }
</script>

 <asp:literal id="litMessage" runat="server" />
AnthonyWJones