Thursday, February 18, 2010

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

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.

The <%# expressions can be used as properties in server-side controls. <%= expressions cannot.















Equals: <%= this.TestValue %>




Pound: <%# this.TestValue %>




Equals label:




Pound label:











//And the code behind is:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
_testValue = "2";
}

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
// DataBind();
_testValue = "3";
}

public string TestValue
{
get { return _testValue; }
}

private string _testValue = "1";
}

No comments: