Posted By

tomaszsimon on 12/21/09


Tagged

control dynamic property lt


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

digiteyes


The difference between <%= and <%# in ASP.NET


 / Published in: C#
 

URL: http://blogs.msdn.com/dancre/archive/2007/02/13/the-difference-between-lt-and-lt-in-asp-net.aspx

  • The <%= expressions are evaluated at render time
    • The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
    • <%# expressions can be used as properties in server-side controls. <%= expressions cannot.

For controls add DataBind() in PreRender Event

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7. <title>Untitled Page</title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12. <p>Equals: <%= this.TestValue %></p>
  13. <p>Pound: <%# this.TestValue %></p>
  14. <p>Equals label: <asp:Label runat="server" ID="_equals" Text="<%= this.TestValue %>" /></p>
  15. <p>Pound label: <asp:Label runat="server" ID="_pound" Text="<%# this.TestValue %>" /></p>
  16. </div>
  17. </form>
  18. </body>
  19. </html>
  20.  
  21. //And the code behind is:
  22.  
  23. public partial class _Default : System.Web.UI.Page
  24. {
  25. protected void Page_Load(object sender, EventArgs e)
  26. {
  27. _testValue = "2";
  28. }
  29.  
  30. protected void Page_PreRenderComplete(object sender, EventArgs e)
  31. {
  32. // DataBind();
  33. _testValue = "3";
  34. }
  35.  
  36. public string TestValue
  37. {
  38. get { return _testValue; }
  39. }
  40.  
  41. private string _testValue = "1";
  42. }

Report this snippet  

You need to login to post a comment.