470,636 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

What is the diffreence between <%= %> and <%# %>

ad
I find there are some tag like <%= %> and <%# > in my .aspx file.

What is the diffreence between <%= %> and <%# %>
Nov 19 '05 #1
5 1756
<%="bla" %> is equal to Response.Write("bla");
<%# %> means that you are binding against an object (e.g. this is
databinding syntax).

----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl

Nov 19 '05 #2
ad
Thank for your immediate answer.

I have still a question:

In some text book for ASP.NET, they use

<%# Container.DataItem("FieldName") %>

to present the filed data, but In ohters, they uese

<%# DataBinder.Eval(Container, "DataItem.FieldName") %>

What is the difference between them?


"Wilco Bauwer" <wi****@gmail.com>
???????:11**********************@g14g2000cwa.googl egroups.com...
<%="bla" %> is equal to Response.Write("bla");
<%# %> means that you are binding against an object (e.g. this is
databinding syntax).

----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl

Nov 19 '05 #3
When you are data binding you need to fetch the value of the object you're
databinding to. Typically this is a row from a database, and for simplicity
I'll keep that as my example. So you then need a column value form the row.
This is the typical syntax for that:

<%# DataBinder.Eval(Container.DataItem, "SomeColumn")%>

The DataBinder.Eval syntax is used to dynamically fetch a value from an object
(the row, in this case), and the value is specified by the string parameter.
The value in this case is the column name. This mechanism is used for flexibility,
since you could be using a DataReader, a DataSet, DataTable, DataView, or
any other custom object to bind to. It uses reflection to fetch the value.

Container is the control that was created for this row in the databinding
control. DataGridItem, GridViewRow, RepeaterItem, whatever. Container.DataItem
is the row you're databinding to. The control holds the rows that was used
to do the data binding.

You can use other syntaxes to do data binding, but they tend to be more tightly
couple type-wise with the data source you're binding to, and thus the syntax
sometimes requires casting and it makes it uglier (if you can believe it)
than the DataBinder.Eval syntax above.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Thank for your immediate answer.

I have still a question:

In some text book for ASP.NET, they use

<%# Container.DataItem("FieldName") %>

to present the filed data, but In ohters, they uese

<%# DataBinder.Eval(Container, "DataItem.FieldName") %>

What is the difference between them?

"Wilco Bauwer" <wi****@gmail.com>
???????:11**********************@g14g2000cwa.googl egroups.com...
<%="bla" %> is equal to Response.Write("bla");
<%# %> means that you are binding against an object (e.g. this is
databinding syntax).
----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl


Nov 19 '05 #4
If you use
<%# Container.DataItem("FieldName") %>
the field's content will, simply, be displayed.

If you use
<%# DataBinder.Eval(Container, "DataItem.FieldName") %>
the field's content will be evaluated and will be returned,
depending on the field's content.

For example, if you use :
<img src='\images\<%#
Databinder.Eval(Container.DataItem,"FieldName")%>. jpg'>
with a boolean ( true/false ) field, if the boolean field returns 1,
the 1.jpg (true) image is displayed and if the boolean field returns 0,
the 0.jpg (false) image is shown.


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thank for your immediate answer.

I have still a question:

In some text book for ASP.NET, they use

<%# Container.DataItem("FieldName") %>

to present the filed data, but In ohters, they uese

<%# DataBinder.Eval(Container, "DataItem.FieldName") %>

What is the difference between them?


"Wilco Bauwer" <wi****@gmail.com>
???????:11**********************@g14g2000cwa.googl egroups.com...
<%="bla" %> is equal to Response.Write("bla");
<%# %> means that you are binding against an object (e.g. this is
databinding syntax).

----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl


Nov 19 '05 #5
In addition to the other good advice you've received, I have an
article with some example code that shows what is happening underneath
the covers:

http://odetocode.com/Articles/278.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sun, 27 Mar 2005 08:38:58 +0800, "ad" <ad@wfes.tcc.edu.tw> wrote:
Thank for your immediate answer.

I have still a question:

In some text book for ASP.NET, they use

<%# Container.DataItem("FieldName") %>

to present the filed data, but In ohters, they uese

<%# DataBinder.Eval(Container, "DataItem.FieldName") %>

What is the difference between them?


"Wilco Bauwer" <wi****@gmail.com>
???????:11**********************@g14g2000cwa.goog legroups.com...
<%="bla" %> is equal to Response.Write("bla");
<%# %> means that you are binding against an object (e.g. this is
databinding syntax).

----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl


Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Francesco Moi | last post: by
1 post views Thread by Christian Schmidbauer | last post: by
4 posts views Thread by matatu | last post: by
2 posts views Thread by Donald Firesmith | last post: by
2 posts views Thread by Francesco Moi | last post: by
34 posts views Thread by Mark Moore | last post: by
6 posts views Thread by tentstitcher | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.