ASP.NET Cafe
Tips and Tricks

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

Tuesday, 18 December 2007 06: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.

Currently rated 4.0 by 6 people

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

Related posts

Comments

March 7. 2008 11:51

John S.

<%# %> can be used anywhere on the page, only the Eval() and Bind() methods are limited to the databound control templates.

For instance, you can do <%# _someValue %> and hook it up with

protected _someValue = "foo";
protected Page_Load(...) {
Page.DataBind();
}

John S.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

August 27. 2008 22:53