.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

Display related records through link in gridview part1

Posted By:satyapriyanayak       Posted Date: May 02, 2013    Points: 40    Category:    URL: http://www.dotnetspark.com  

In some websites some data are present in the inner pages. Suppose we want to find a person all details, but when we initially visit only its id (link) or name as a link is show to the user. When we click on that link another page appears with his all detailed data about him.
 

In some websites some data are present in the inner pages. Suppose we want to find a person all details, but when we initially visit only its id (link) or name as a link is show to the user. When we click on that link another page appears with his all detailed data about him.

Here in one gridview all the name, salary and address will show, and a view details link. When we click view details link all there details will be shown in a table format.

test1. aspx code

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

<!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">
    <asp:GridView ID="g1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField HeaderText="Employee name" DataField="empname"/>
    <asp:BoundField HeaderText="Employee Salary" DataField="empsal"/>
    <asp:BoundField HeaderText="Employee Address" DataField="empaddress"/>
   
    <asp:HyperLinkField HeaderText="View Details Information of Employee" DataNavigateUrlFields="empid" DataNavigateUrlFormatString="test2.aspx?details={0}" Text="Employee Details"/>
    </Columns>
        <HeaderStyle ForeColor="#FF0066" />
        <AlternatingRowStyle ForeColor="#CC6600" />
    </asp:GridView>
    </form>
</body>
</html>


test1.aspx.vb code

Imports System.Data
Imports System.Data.SqlClient
Partial Class test1
    Inherits System.Web.UI.Page
    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
    Dim con As New SqlConnection(strConnString)
    Dim str As String
    Dim com As SqlCommand
    Dim sqlda As SqlDataAdapter
    Dim ds As DataSet
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        bind()
    End Sub
    Sub bind()
        con.Open()
        str = "select * from employee"
        com = New SqlCommand(str, con)
        Dim reader As SqlDataReader
        reader = com.ExecuteReader()
        g1.DataSource = reader
        g1.DataBind()
        con.Close()
    End Sub
End Class





test2.aspx code

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

<!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>
    <table border="1" style="border-collapse: collapse"  cellspacing="1">
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Empname:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Empaddress:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label2" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Empsal:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label3" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Empphone:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label4" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Empfax:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label5" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Empcity:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label6" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Empstate:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label7" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Empzip:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label8" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Emplic:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label9" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    <tr>
      <td width="77" height="16" align="left" ><b><font size="2" color="red">Empstatus:</font></b></td>
      <td width="77" height="16" align="left" ><b><font size="2">&nbsp;<asp:Label ID="Label10" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    </table>

    </div>
   
    </form>
</body>
</html>

test2.aspx.vb code


Imports System.Data
Imports System.Data.SqlClient
Partial Class test2
    Inherits System.Web.UI.Page
    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
    Dim con As New SqlConnection(strConnString)
    Dim str As String
    Dim com As SqlCommand
    Dim sqlda As SqlDataAdapter
    Dim ds As DataSet
    Dim s1 As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            con.Open()
            s1 = Request.QueryString(0)
            str = "select * from employee where empid='" + s1 + "'"
            com = New SqlCommand(Str, con)
            Dim reader As SqlDataReader
            reader = com.ExecuteReader
            If reader.Read Then
                Label1.Text = reader("empname")
                Label2.Text = reader("empaddress")
                Label3.Text = reader("empsal")
                Label4.Text = reader("empphone")
                Label5.Text = reader("empfax")
                Label6.Text = reader("empcity")
                Label7.Text = reader("empstate")
                Label8.Text = reader("empzip")
                Label9.Text = reader("emplic")
                Label10.Text = reader("empstatus")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend