Wednesday, 7 July 2010

THE DIFFERENCE BETWEEN <%= AND <%# IN ASP.NET

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. DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5. <title>Untitled Pagetitle>
  6. head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <p>Equals: <%= this.TestValue %>p>
  11. <p>Pound: <%# this.TestValue %>

  12. <p>Equals label: <asp:Label runat="server" ID="_equals" Text="<%= this.TestValue %>" />p>
  13. <p>Pound label: <asp:Label runat="server" ID="_pound" Text="<%# this.TestValue %>" />p>
  14. div>
  15. form>
  16. body>
  17. html>
  18. //And the code behind is:
  19. public partial class _Default : System.Web.UI.Page
  20. {
  21. protected void Page_Load(object sender, EventArgs e)
  22. {
  23. _testValue = "2";
  24. }
  25. protected void Page_PreRenderComplete(object sender, EventArgs e)
  26. {
  27. // DataBind();
  28. _testValue = "3";
  29. }
  30. public string TestValue
  31. {
  32. get { return _testValue; }
  33. }
  34. private string _testValue = "1";
  35. }

No comments:

Post a Comment