Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

.net - In ASP.NET, what are the different ways to inline code in the .aspx?

Can I get a 'when to use' for these and others?

<% %>
<%# EVAL() %>

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check out the Web Forms Syntax Reference on MSDN.

For basics,

  • <% %> is used for pure code blocks. I generally only use this for if statements


      <div class="authenticated">

      <div class="unauthenticated">
  • is used to add text into your markup; that is, it equates to

    <div class='<%= IsLoggedIn ? "authenticated" : "unauthenticated" %>'>

  • <%# Expression %> is very similar to the above, but it is evaluated in a DataBinding scenario. One thing that this means is that you can use these expressions to set values of runat="server" controls, which you can't do with the <%= %> syntax. Typically this is used inside of a template for a databound control, but you can also use it in your page, and then call Page.DataBind() (or Control.DataBind()) to cause that code to evaluate.

The others mentioned in the linked article are less common, though certainly have their uses, too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...