How to embed C # code on the server in HTML

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.