Showing posts with label datalist. Show all posts
Showing posts with label datalist. Show all posts

Eval() and Bind() Two-Way DataBinding Expressions in Asp.Net

The ASP.NET Framework supports both one-way DataBinding expressions and two-way DataBinding expressions.

In a oneway DataBinding expression, you use the DataBinding expression to display the value of a data item. You use the Eval() method to display the value of a one-way DataBinding expression.

In a two-way DataBinding expression, you not only can display the value of a data item,
you also can modify the value of a data item. You use the Bind() method when working with a    two-way DataBinding expression.

Example:

Suppose you have a movies table in your database. It has columns named Id, Title, Director, DateReleased.

<asp:FormView id="FormView1" DataKeyNames="Id" DataSourceId="srcMovies" DefaultMode="Edit" AllowPaging="true" Runat="server">
<EditItemTemplate>

<asp:Label
id="lblTitle"
Text="Title:"
AssociatedControlID="txtTitle"
Runat="server" />

<asp:TextBox
id="txtTitle"
Text='<%#Bind(“Title")%>'
Runat="server" />
<br />

<asp:Label
id="lblDirector"
Text="Director:"
AssociatedControlID="txtDirector"
Runat="server" />

<asp:TextBox
id="txtDirector"
Text='<%#Bind(“Director")%>'
Runat="server" />
<br />

<asp:Button
id="btnUpdate"
Text="Update"
CommandName="Update"
Runat="server" />
</EditItemTemplate>
</asp:FormView>

<asp:SqlDataSource id="srcMovies" ConnectionString="yourConnectionString"
SelectCommand="SELECT Id, Title,Director,DateReleased FROM Movies"
UpdateCommand="UPDATE Movies SET Title=@Title,
Director=@Director WHERE Id=@Id"
Runat="server" />

The FormView contains an EditItemTemplate. The EditItemTemplate contains three TextBox controls. Each TextBox control has a two-way DataBinding expression assigned to its Text property.

The DataBinding expressions associate the TextBox control properties with the properties of the data item being edited. When you click the Update button, any changes you make to the Text properties are updated in the Movies database table.


Read more...

DataBinding Expressions in Asp.Net

A DataBinding expression is a special type of expression not evaluated until runtime. You mark a databinding expression in a page by wrapping the expression in opening <%# and closing %> brackets.

A DataBinding expression isn’t evaluated until a control’s DataBinding event is raised.

When you bind a DataBound control to a DataSource control declaratively, this event is raised automatically. When you bind a DataSource control to a data source programmatically, the DataBinding event is raised when you call the DataBind() method.

Example:

Suppose you have a movies table in your database. It has two columns named "Title" and "DateReleased".

<asp:DataList id="DataList1" DataSourceId="srcMovies" Runat="server">
    <ItemTemplate>
    <b>Movie Title:</b> <%#Eval("Title")%>
    <br />
    <b>Date Released:</b> <%#Eval("DateReleased", "{0:D}") %>
    <hr />
    </ItemTemplate>
    </asp:DataList>

<asp:SqlDataSource
id="srcMovies"
ConnectionString="your connection string"
SelectCommand="SELECT Title,DateReleased FROM Movies"
Runat="server" />


The first DataBinding expression displays the title of the movie and the second DataBinding expression displays the date the movie was released. Both DataBinding expressions call the Eval() method. The Eval() method is a protected method of the Page class. Behind the scenes, the Page.Eval() method calls the static
(shared) DataBinder.Eval() method. If you want to be verbose, instead of using the Eval() method, you could use the following two expressions:

<%# DataBinder.Eval(Container.DataItem, “Title”) %>
<%# DataBinder.Eval(Container.DataItem, “DateReleased”, “{0:D}” ) %>

In ASP.NET version 1.x, you had to use DataBinder.Eval() when displaying data items in a template.
You can use <%# FormatTitle(Eval(“Title”)) %>. This method formats each of the titles displayed by the Repeater control by making each title bold and uppercase

Note: Technically, the Eval() method uses reflection when evaluating the data item to find
a property with a certain name. You do pay a performance penalty when you use
reflection.

As an alternative, you can improve the performance of your DataBinding expressions by casting the data items to a particular type like this:

<%# ((System.Data.DataRowView)Container.DataItem)[“Title”] %>

Read more...