简体   繁体   中英

ASP.Net <%# %> and <%= %> rules?

Can someone explain to me the rules around what can and cannot be evaluated/inserted into markup using the <%# %> and <%= %> tags in asp.net?

When I first discovered I could inject code-behind variables into mark-up using <%= I thought 'great'. Then I discovered that if such tags are present you can then not add to the controls collection of the page (that's a whole different question!). But <%# tags are ok.

Is there a way I can inject a code-behind variable or function evaluation into the page using <%# ? Thanks.

<%%> are code blocks. You can put any server side code in them. This is a shortcut for <script runat="server"></script> .

<%=%> is for outputting strings. This is a shortcut for <script runat="server">Response.Write()</script> .

See here for more detail about <%%> and <%=%> .

<%#%> are used for data binding expressions .

See the index page for asp.net Page syntax.

The <%# inline tag is used for data binding, so if you want to use a code-behind variable inside it, you will have to bind the page or control the variable resides in:

Page.DataBind();

You can include this statement in the Page_Load or Page_PreRender event.

See this article for more information about the use of inline tags in ASP.Net, and this article for more about server-side databinding.

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