How to embed C # code on the server in HTML
1 <% = variable or function %>
Example: <input value = "<% = myName %>"/>
Features: This code can only be written in non-server-side code. If it is written in a control with runat = "server", it will not work.
2 <% # variable or function %>
Example: <asp: Button ID = "butTest" runat = "server" Text = "<% # myButName %>"/>
Feature: You need to call the binding method and call Page. DataBind () in the pageload event on the background Page ();
3 <script runat = "server">
String strTest = "output on the foreground page! ";
Protected void Page_Load (object sender, EventArgs e)
{
Response. Write (strTest );
}
</Script>
This is rarely used. If you want to write this code, you can directly write the code in the background.
4 <% c # code %>
This is the code of the earliest ASP era, which is still supported by ASP. NET, but is rarely used.
For example, if you need to bind a background variable to the foreground and apply the corresponding control object to the background, you need to set the control to runat = "server ", at this time, the first syntax does not work and does not want to bind it in the 2nd method. In this case, you can use the client js to implement it and use the server-side variables in js, then, update the foreground controls.
For example:
<Input id = "txtPageSize" runat = "server" style = "width: 28px; border-left: 0px; border-right: 0px; border-top: 0px; border-bottom: 1px solid # dddddddd; text-align: center; "/> entries
<Script type = "text/javascript">
Document. getElementByIdx_x ("txtPageSize"). value = <% = pageBar. PageSize %>;
</Script>