<%= %> vs. <%# %>

  • Thread starter Thread starter Neil Zanella
  • Start date Start date
N

Neil Zanella

Hello,

I would like to know what the difference is among the constructs
<%= %> for evaluating an expression and displaying the evaluated
result on the page and <%# %>. In particular I would like to know
why the <%# %> construct is necessary in locations where the other
<%= %> does not do. It seems to me that there is no equivalent of
the <%# %> construct in PHP, thus I thought I would post to
clarify. Is this an ASP.NET specific construct not present
in ASP? Thanks for the clarification.

Best Regards,

Neil
 
Neil said:
I would like to know what the difference is among the constructs
<%= %> for evaluating an expression and displaying the evaluated
result on the page and <%# %>.

<%# %> is the ASP.NET data-binding expression. You can include
data-binding expressions on the value side of an attribute/value pair in
the opening tag of a server control or anywhere else in a page.

<%= %> is shorthand for Response.Write.

The Response.Write expressions are evaluated when the page is processed.
The data-binding expressions create bindings between any data source,
property, collection, expression or result returned from a method call
when the page's DataBind method is called.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Thank you for your reply,

However I am experiencing something strange:

The following is working fine:

<asp:TextBox Text=<%# "foo" + DataBinder.Eval
(Container.DataItem, "bar").ToString() %> runat="server"/>

but if I want to use this way to set the ID attribute it
does not seem possible, so I am not sure what to do, maybe
I should check out the CommandText property???

<asp:TextBox ID=<%# "foo" + DataBinder.Eval
(Container.DataItem, "bar").ToString() %> runat="server"/>

Parser Error Message: '<%# "foo" + DataBinder.Eval(Container.DataItem,
"bar").ToString() %>' is not a valid identifier.

Why this error? Why can't I just set the value of the
ID property in this way like I can set everything else???

Thanks,

Neil
 
Thank you for your reply,

However I am experiencing something strange:

The following is working fine:

<asp:TextBox Text=<%# "foo" + DataBinder.Eval
(Container.DataItem, "bar").ToString() %> runat="server"/>

but if I want to use this way to set the ID attribute it
does not seem possible, so I am not sure what to do, maybe
I should check out the CommandText property???

<asp:TextBox ID=<%# "foo" + DataBinder.Eval
(Container.DataItem, "bar").ToString() %> runat="server"/>

Parser Error Message: '<%# "foo" + DataBinder.Eval(Container.DataItem,
"bar").ToString() %>' is not a valid identifier.

Why this error? Why can't I just set the value of the
ID property in this way like I can set everything else???

I guess the ID is necessary for viewstate purposes, and shouldn't be
tempered with at runtime.

If you need some tag to identify your textbox, use the CommandArgument
property.
 
Why this error? Why can't I just set the value of the
ID property in this way like I can set everything else???
No. The identifier is used to uniquely identify the control within the
page. Therefore, the id must be unique when the page is parsed. Since
the page is parsed before data binding takes place, it cannot be data bound.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Back
Top