By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

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

P: n/a
ad
I find there are some tag like <%= %> and <%# > in my .aspx file.

What is the diffreence between <%= %> and <%# %>
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<%="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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.