ARTICLE

Table control link data is clicked then corresponding values will be shown in datagrid in VB.NET

Posted by Satyapriya Nayak Articles | ASP.NET using VB.NET August 10, 2011
Here I will show you, data will be bind to a table control as a link. When we will click this link all the respective data related to this link will we shown in a gridview.
Reader Level:

Here I will show you, data will be bind to a table control as a link. When we will click this link all the respective data related to this link will we shown in a gridview.

Program

Default.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

    </div>

    <asp:Table ID="Table1" runat="server" BackColor="#FFFF80" BorderColor="#FF8080"

        BorderStyle="Solid" BorderWidth="5px" ForeColor="Red" GridLines="Both">

        <asp:TableRow ID="TableRow1" runat="server">

            <asp:TableCell ID="TableCell1" runat="server">Student Id</asp:TableCell>

           

        </asp:TableRow>

    </asp:Table><br />

    </form>

</body>

</html>

 

Default.aspx.vb code

Imports System.Data.SqlClient

Imports System.Data

Partial Class _Default

    Inherits System.Web.UI.Page

    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()

    Dim sqlda As SqlDataAdapter

    Dim com As SqlCommand

    Dim ds As DataSet

    Dim dt As DataTable

    Dim str As String

 

    Dim con As New SqlConnection(strConnString)

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

        con.Open()

        str = "select * from student"

        com = New SqlCommand(str, con)

        sqlda = New SqlDataAdapter(com)

        ds = New DataSet

        sqlda.Fill(ds, "student")

        dt = ds.Tables("student")

        con.Close()

 

        Dim i As Integer

        For i = 0 To dt.Rows.Count - 1

 

            Dim tr As New TableRow

            Dim tc As New TableCell

            tc.Text = "<a href=Default2.aspx?id=" & ds.Tables(0).Rows(i).Item(0) & ">" & ds.Tables(0).Rows(i).Item(0) & " </a>"

            tr.Cells.Add(tc)

            Table1.Rows.Add(tr)

        Next

    End Sub

End Class

Default2.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div> 

    </div>

    <asp:GridView ID="GridView1" runat="server" BackColor="#FF9999">

        <HeaderStyle BackColor="#FFCC99" />

    </asp:GridView>

    </form>

</body>

</html>

Default2.aspx.vb code

Imports System.Data.SqlClient

Imports System.Data

Partial Class Default2

    Inherits System.Web.UI.Page

    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()

    Dim sqlda As SqlDataAdapter

    Dim com As SqlCommand

    Dim ds As DataSet

    Dim dt As DataTable

    Dim str As String

    Dim con As New SqlConnection(strConnString)

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

        con.open()

        Str = "select * from student where sid='" & Request.QueryString("id") & "'"

 

        com = New SqlCommand(Str, con)

        sqlda = New SqlDataAdapter(com)

        ds = New DataSet

        ds.Clear()

        ds.Reset()

        sqlda.Fill(ds, "student")

        GridView1.DataMember = "student"

        GridView1.DataSource = ds

        GridView1.DataBind()

        con.Close()

    End Sub

End Class

Output

 satyapriya1.gif

 satyapriya2.gif

Thanks for reading.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor