ASP. NET foreground code bind background variable method

Source: Internet
Author: User

Webpage special effect code

The location of the html display content (that is, the content between the start tag and the end tag, for example, <div> here </div> (html Tag) or <asp Tutorial: label id = "label2" runat = "server" text = "label"> here </asp: label> (server-side control) is used as a placeholder to display variables in

For the first position, there are some constraints:

(1) The general attribute requirements are string or numeric. (Some server attributes support data sets );

(2) Not all attributes can be bound to variables. For example, the runat attribute must be a constant of "server". Even if the bound string is a server, an error occurs during analyzer analysis;

(3) There is an attribute that requires the attribute value to be constrained (type constraints, for example, the server-side control requires that the tabindex attribute be of the short type, or the string content is constrained ), it should also be met during binding; otherwise, an error may still be reported during compilation;

(4) another property. Although the property itself has constraints, it can be compiled even if the bound variable does not meet the constraints, such as the checked attribute of input, only the checked string is valid, but if the string obtained by binding is not checked, these attributes will have their own internal processing mechanism to ensure normal use;

(5) Pay attention to the fact that, even for the same class of attributes, the processing mechanisms of the server side and the html attributes are also different. The same is tabindex (tabindex). If the former does not meet the requirements, the analyzer is incorrect, the latter ignores this issue.

For the second position, it is generally only required that the bound background variable is compatible with the Data Type in javascript.

For the third position, if the binding position does not exist in the server-side control, there is no constraint. As long as it is a constant string that can appear, it can be bound. However, for the control placed inside the server, that is, the <asp: label id = "label2" runat = "server" text = "label"> here </asp: label>, there are constraints. The summary is summarized into four types of server-side controls. If the bound code appears between the start and end labels of these controls (the control mentioned here, if the bound code is surrounded by multiple layers of nested controls, it refers to the inner control of the bound code), with different display results:

(1) Constrained controls: these controls require that their start and end labels only contain the specified child controls. Therefore, if a code block appears here, a compilation error occurs. For example:

<Asp: datalist runat = "server"> </asp: datalist>. The <itemtemplate> </itemtemplate> must be nested between them.

(2) Non-nested controls: such controls cannot be embedded with other controls or labels, but can only be constant strings, it uses the constant string content in the start tag and end tag as its attribute. For example, the textbox mentioned above uses the content between tags as its text attribute value.

(3) nested controls: these controls can be nested with any other controls or contain strings. Therefore, the string content represented by the bound code block can be displayed normally. Such as label control and panel.

(4) data binding controls: these controls are server-side controls provided by the asp.net tutorial. In addition to common variable types, you can also bind a data set (which can only be implemented in the second method below ).

Quotation marks: Should <% = str %> or <% # str %> be placed in single or double quotation marks when using the preceding three positions? For different locations, the processing method is different: (Please refer to the following two methods for details)

(1) For the first position, because javascript is weak, it is always correct to treat it as a string if quotation marks are added during binding; if no quotation marks are added during binding, it will be considered as a numerical value. If the obtained value is a real value, of course, if it is not a numeric value, a script error will be generated, this is the same even when a constant is assigned to javascript:

Reference content is as follows:

Var test1 = 123b; // an error is reported during running.
Var test2 = 123; // correct, which is a numeric value
Var test3 = "123b"; // correct, string type

The front-end is common. aspx file. The background refers to the codebehind associated with aspx. The file suffix is. aspx. cs; on the other hand, binding here means that after a user sends a command to access a page, the server has assigned a value to the front-end code during execution, after the html format is generated, it is returned to the client for display, instead of being displayed to the client. Then, you can use other methods (such as ajax) to obtain the corresponding variables on the server.

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.