ARTICLE

DataBinder.Eval Method in VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET July 22, 2011
Here we will see how to use DataBinder.Eval Method in VB.NET.
 
Reader Level:

This method is used to evaluates data-binding expressions at run time and formats the result as a string.

Syntax

DataBinder.Eval Method (Object, String, String)

DataBinder.Eval takes 2 or 3 arguments. The first arg is the data object to bind to. In the case of DataGrid, DataList and Repeater, Container.DataItem is the single row. The second arg the string name of the field from the data object you which to display. DataBinder.Eval uses these two pieces of information to work out the rest of the expression syntax.

For example

In this example we create a database table with title, description and author name. Title link will be create in the asp .net GridView control. when we click on the title link it will be redirect to the dynamic page.

Create a table in database named as Articletable. Table looks like this.

CREATE TABLE [dbo].[Articletable](

      [ID] [int] NOT NULL,

      [Title] [varchar](200) NULL,

      [Description] [varchar](400) NULL,

      [Author] [varchar](50) NULL

)

Inserting data in the articletable.

INSERT INTO Articletable VALUES(1,'How to validate dropdownlist in asp.net','Here, we will learn how to validate a DropDownList in ASP.NET.','Rohatash Kumar');

GO

INSERT INTO Articletable VALUES(2,'Introduction to .NET Assemblies in VB.NET',' Here is a comprehensive introduction to .NET assemblies.','sunil Kumar');

go

INSERT INTO Articletable VALUES(3,'BinaryReader and BinaryWriter classes in VB.NET','In this article I will explain about BinaryReader and BinaryWriter Classes in VB.NET.','Deepak Kumar');

 

go

INSERT INTO Articletable VALUES(4,'StreamWriter class in VB.NET','This article shows how to create a new text file and write a string to it.','Rohatash Kumar');

go

select * from articletable;

OUTPUT

rewrite1.jpg

Now in ASP. NET

  1. Open Visual Studio.
  2. Add two webForm to your website, name it Gridview.aspx.

Now drag and drop a GridView control from the Toolbox on the gridview.aspx page.

Now Using DataBinder Method

<asp:GridView ID="GridView12" runat="server" AutoGenerateColumns="False"

            BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"

            CellPadding="3" CellSpacing="2" Width="788px" Height="80px">

            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />

            <Columns>

                <asp:TemplateField HeaderText="Title">

                    <ItemTemplate>

                        <asp:HyperLink ID="hlTitle" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Title")%>'></asp:HyperLink>                       

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Description">

                    <ItemTemplate>

                        <asp:Label ID="lblDesc" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Description")%>'></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>

 

                <asp:TemplateField HeaderText="Author">

                    <ItemTemplate>

                        <asp:Label ID="lblauthor" runat="server"  Text='<%#DataBinder.Eval(Container.DataItem,"Author")%>'></asp:Label>                       

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />

            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />

            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />

            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

        </asp:GridView>

Gridview.aspx

The form looks like this.

Gridview with binder.eval method

Figure1

Now add the following code with page load event.

Imports System.Data.SqlClient

Imports System.Data

Public Class Gridview

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        GridView12.DataSource = GetData()

        GridView12.DataBind()

 

    End Sub

    Private Function GetData() As DataTable

        Dim strConn As String = ("Data Source=.; uid=sa; pwd=Password$2; database=userinfo")

        Dim conn As New SqlConnection(strConn)

        Dim da As New SqlDataAdapter("select Id,Title,Description,author from Articletable", conn)

        Dim ds As New DataSet()

        da.Fill(ds, "Articletable")

        Return ds.Tables("Articletable")

    End Function

 

End Class

Now run the application and test it.

Gridview with record binding

Figure2

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor