简体   繁体   中英

Asp.net how to write c# code inside aspx page?

This is my code

 <ul>
   <% foreach (string str in more())
    {%>
     <li><% $str %>  </li>      
    <% } %>
  </ul>

when i run the code there is an error <% $ str %> are not allowed how can place str inside list tag? thanks in advance

You can make the property public and put it in the page load event using code behind.

You can reference the property like this <%= str %>

using variables in asp.net

You actually cant write it PHP style. ASP WebForms has a another stucture where code behind(logic) and view(aspx) are two separate files. The binding of the data to the view happens by actually binding your dataset to a servercontrol.

An example Add a new server control to your aspx page:

<asp:label runat="server" ID="lblMyLabel"/>

And in the aspx.cs file change(or add if there are none) the Page_Load event:

 protected void Page_Load(object sender, EventArgs e) {
        lblMyLabel.Text="Hello World";
    }

As you see you can access the controls directly from the code. The whole thing is event based, so you can access your controls at different execution time. Read into this article to know more about this: http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx You can register your own event handlers(and events) for the controls to react to user input or do your workflow.

EDIT: Yes you can use the "<%= %>", Eval() and <%# %> to access public variables from the view but this is not a good coding style.

I hope i could help you.

Creating a single page aspx file with the code embedded can be very useful if you need to provide clients with some way of debugging their webserver/application configuration.

All that is required is to bracket your code with...

<script runat="server">
    protected void Page_PreInit(object sender, EventArgs e) {
    }
</script>

If their webserver won't even load your debug script then the issue is definitely on their side.

A starter template I use is as follows... My version adds logging and more details.

<%@ Page Language="C#" %>
<!DOCTYPE html>
<%@Import Namespace="System.IO"%>
<script runat="server">
    public static string getProcessName()
    {
            return "Debug Script";
    }
    protected void Page_PreInit(object sender, EventArgs e) {
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Debug Script</title>
</head>
<body>
<h1>Debug Script</h1>
    <table>
        <thead><tr><th>Item/Test</th><th>Result</th></tr></thead>
        <tbody>
            <tr><th>Process</th><td><%=getProcessName() %></td></tr>
            <tr><th>App Pool</td><td><%=System.Environment.GetEnvironmentVariable(
                  "APP_POOL_ID", EnvironmentVariableTarget.Process) %></td></tr>            
            <tr><th>Is 64 Bit</th><td><%=Environment.Is64BitProcess %></td></tr>
        </tbody>
    </table>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM