Hi vereato,
Please refer below sample code.
Database
CREATE TABLE [Actors]
(
[Id] [int] NOT NULL PRIMARY KEY,
[ActorName] [nchar](50) NOT NULL
)
GO
INSERT [dbo].[Actors] ([Id], [ActorName]) VALUES (1, N'Jason Statham')
INSERT [dbo].[Actors] ([Id], [ActorName]) VALUES (2, N'Bruce Lee')
INSERT [dbo].[Actors] ([Id], [ActorName]) VALUES (3, N'Van Dame')
GO
CREATE TABLE [Movies]
(
[Id] [int] NOT NULL PRIMARY KEY,
[ActorId] [int] NOT NULL,
[MovieName] [nchar](50) NOT NULL
)
GO
INSERT [dbo].[Movies] ([Id], [ActorId], [MovieName]) VALUES (101, 1, N'The Transporter')
INSERT [dbo].[Movies] ([Id], [ActorId], [MovieName]) VALUES (102, 1, N'Wrath of Man')
INSERT [dbo].[Movies] ([Id], [ActorId], [MovieName]) VALUES (103, 2, N'Bruce Lee The Fighter')
INSERT [dbo].[Movies] ([Id], [ActorId], [MovieName]) VALUES (104, 2, N'Bruce Lee The Myth')
INSERT [dbo].[Movies] ([Id], [ActorId], [MovieName]) VALUES (105, 3, N'Kill them All')
INSERT [dbo].[Movies] ([Id], [ActorId], [MovieName]) VALUES (106, 3, N'the Quest')
GO
HTML
Default
<form runat="server">
<asp:DataList ID="dlActors" runat="server" RepeatColumns="2">
<ItemTemplate>
<table>
<tr>
<td>
<asp:ImageButton ID="imgActor" runat="server" Height="150" Width="150" OnClick="OnDisplay"
ImageUrl='<%# string.Format("/Images/{0}.jpg", Eval("Id")) %>' />
</td>
</tr>
<tr>
<td>
<asp:HiddenField ID="hfId" runat="server" Value='<%# Eval("Id") %>' />
<asp:Label ID="lblActorName" runat="server" Text='<%# Eval("ActorName") %>' Font-Bold="true" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</form>
View
<form id="form1" runat="server">
<h3>List of Movies</h3>
<ul>
<asp:Repeater runat="server" ID="rptMovies">
<ItemTemplate>
<li>
<asp:Label ID="lblMovieName" runat="server" Text='<%# Eval("MovieName") %>' />
</li>
</table>
</ItemTemplate>
</asp:Repeater>
</ul>
</form>
Namespace
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Actors", con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dlActors.DataSource = dt;
dlActors.DataBind();
}
}
}
}
}
protected void OnDisplay(object sender, ImageClickEventArgs e)
{
DataListItem item = (sender as ImageButton).NamingContainer as DataListItem;
Session["Id"] = (item.FindControl("hfId") as HiddenField).Value;
Response.Redirect("~/View.aspx");
}
View
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Movies where ActorId = @Id", con))
{
cmd.Parameters.AddWithValue("@Id", Session["Id"]);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
rptMovies.DataSource = dt;
rptMovies.DataBind();
}
}
}
}
}
}
VB.Net
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Actors", con)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
dlActors.DataSource = dt
dlActors.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub OnDisplay(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Dim item As DataListItem = TryCast((TryCast(sender, ImageButton)).NamingContainer, DataListItem)
Session("Id") = (TryCast(item.FindControl("hfId"), HiddenField)).Value
Response.Redirect("~/View.aspx")
End Sub
View
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Movies where ActorId = @Id", con)
cmd.Parameters.AddWithValue("@Id", Session("Id"))
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
rptMovies.DataSource = dt
rptMovies.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Screenshot