For ASP .Net display use databind or implicit Response.Write (<%=

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

For ASP .Net display of a protected value what's better <%# %> (with
DataBinding) or the ASP <%= %>?

Is one more efficent or is there no difference?

Thank you.
 
Hi Siudp,

As for the two expressions you mentioned, they're two different things
which have different usage in asp.net web page.

The <%= %> expression is just dervied from the classic asp, which help to
output content on page(just like the response.Write()). So it is ok to
output a page's protected / public member value via it.

However, the <%# expression... %> is rather different, it's the
databinding expression in asp.net which is used to perform databinding
task. And it is not only used on page but mainly used in many template
databinding controls such as (DataGrid/ DataList)'s template. And the
expressions in the <%# %> will be called when its container control's
"DataBind()" method is called. So if you use the <%# %> on your page, it
will only be called when you call the Page.DataBind() mehtod, then all the
sub control(on the page)'s "DataBind()" will also be called recursively.
For detailed reference on the asp.net databinding expression, you may have
a look at the following msdn doc:

#Data Binding Expression Syntax
http://msdn.microsoft.com/library/en-us/cpgenref/html/cpcondatabindingexpres
sionsyntax.asp?frame=true

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Steven,

Thanks for your reply. I should have been more explicit. I can display a
public or protected variable in ASP .net by using <%# TotalAmount.ToString(
"###,###,##0" ) %> in the ASPX page and calling DataBind(); in the code behind

I am attempting to learn whether or not there is performance or other reason
to instead use <%= TotalAmount %> or is it a draw and merely a matter of
preference.

Thank you
 
Hi Siudp,

Thanks for you response. There is not performance concerns on choosing <%=
%> or <%# %>. Just for different scenarios. As you said, you can use both
<%= %> and <%# .. %> + calling Page.DataBind() to do the same thing.
However, this is ok when your page only contains plain HTML and some <%= %>
or <%# %> blocks. If there is other controls on the page which also
contains <%# %> for databinding, calling Page.DataBind will cause all the
sub controls(subcontrol's subcontrols)' DataBind be fired, but this is
sometimes unnecessary.

In addition, <%# %> databinding expression can help bind values onto
asp.net servercontrol's property such as
<asp:Label ... Text='<%# variable or expression %>' ></asp:Label>

but <%= %> can't not, it is only used to output content on the top page
level.

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
You're welcome. Thanks again for your posting.
Have a good day!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top