ASP.NET DataBinder.Eval Method in VB.NET

Here we will see how to use DataBinder.Eval Method in VB.NET.
  • 5317

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


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','Here, we will learn how to validate a DropDownList in ASP.NET.','Rohatash Kumar');


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


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');



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');


select * from articletable;



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" />


                <asp:TemplateField HeaderText="Title">


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



                <asp:TemplateField HeaderText="Description">


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




                <asp:TemplateField HeaderText="Author">


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




            <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" />



The form looks like this.



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 ObjectByVal e As System.EventArgsHandlesMe.Load

        GridView12.DataSource = GetData()



    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.




More Articles

© 2020 DotNetHeaven. All rights reserved.