ASP.NET Cafe
new tricks every week

ASP.NET For Beginners: This funny <%# and <%= markup

Tuesday, 18 December 2007 01:28 by dmitriy

Everybody who starts to learn ASP.NET techniques ask the question: What is <%= and <%# What is the difference, and how to use this?

First of all, these instructions looks the same. Both outputs something in the place located. But truly output instruction is only <%= , like all Java developers already know. It allows you to output some variable or function result in the place you put it, for example <%= this.FirstName %> . This instruction is good for your user controls (ascx) that represents some view of your data.

What is the second instruction? <%# is much reacher then previous. But it can be used the same way (funny, yes). With only one restriction: it can be used only inside databindable control template. For example, Repeater, Datalist, Datagrid. Usually used to evaluate current data item values in controls template. Example of usage is: <%# Eval("FirstName") %>. That's all ? No, also there are one very powerful thing - Two-way databinding in some controls like FromView and Datagrid. So, how it works. You can put two-way databinding instruction inside some controls property. Look at the following sample: 

<asp:TextBox ID="EMailTextBox" runat="server" Text='<%# Bind("EMail") %>'></asp:TextBox>

And now FormView control not only evaluate value of EMail field, but also update in datasource with new one, if Update or Insert command comes.

This is a basic things you need to know to start using ASP.NET. Usually, this is just review.

You can find more about Two-way databinding in MSDN.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   ASP.NET
Actions: E-mail | del.icio.us | Kick it! | DZone it! | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


 

  Country flag

biuquote
  • Comment
  • Preview
Loading