DetailView Question

M

momo

Guys,

I am trying to created a detailview that displays several records at a time.
Let's say I have a GridView with checkboxes and when I check two checkboxes
and click a button, I want it to give me two DetailViews; one for each
record selected. how can I do this? Or is there a better way than this?
 
K

Ken Cox [Microsoft MVP]

Hi,

I'd create the DetailsView dynamically (add them to a placeholder). Use a
DataView filter to display the desired data. Here's the code below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

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

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not IsPostBack Then
GridView1.DataSource = CreateDataSource()
GridView1.DataBind()
End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim chbx As CheckBox
Dim lit As Literal
For Each gvrow As GridViewRow In GridView1.Rows
chbx = gvrow.FindControl("CheckBox1")
If Not IsNothing(chbx) Then
If chbx.Checked Then
PlaceHolder1.Controls.Add _
(CreateDetailsView(gvrow.DataItemIndex))
lit = New Literal
lit.Text = "<p></p><p></p>"
PlaceHolder1.Controls.Add(lit)
End If
End If
Next
End Sub

Protected Function CreateDetailsView _
(ByVal intID As Integer) As DetailsView
Dim dtvw As New DetailsView
Dim dv As New Data.DataView
Dim dt As Data.DataTable
dt = CreateDataSource()
dv = dt.DefaultView
dv.RowFilter = "IntegerValue = " & intID.ToString
dtvw.DataSource = dv
dtvw.DataBind()
Return dtvw
End Function

Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Create DetailsView Dynamically based on Checkboxes</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="GridView1" runat="server">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="CheckBox1" runat="server" />
</itemtemplate>
</asp:templatefield>
</columns>

</asp:gridview>
<br />
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="OK" /><br />
<br />
&nbsp;<asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder>

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

momo

Thank Ken,

I am going to try it out.



Ken Cox said:
Hi,

I'd create the DetailsView dynamically (add them to a placeholder). Use a
DataView filter to display the desired data. Here's the code below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

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

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not IsPostBack Then
GridView1.DataSource = CreateDataSource()
GridView1.DataBind()
End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim chbx As CheckBox
Dim lit As Literal
For Each gvrow As GridViewRow In GridView1.Rows
chbx = gvrow.FindControl("CheckBox1")
If Not IsNothing(chbx) Then
If chbx.Checked Then
PlaceHolder1.Controls.Add _
(CreateDetailsView(gvrow.DataItemIndex))
lit = New Literal
lit.Text = "<p></p><p></p>"
PlaceHolder1.Controls.Add(lit)
End If
End If
Next
End Sub

Protected Function CreateDetailsView _
(ByVal intID As Integer) As DetailsView
Dim dtvw As New DetailsView
Dim dv As New Data.DataView
Dim dt As Data.DataTable
dt = CreateDataSource()
dv = dt.DefaultView
dv.RowFilter = "IntegerValue = " & intID.ToString
dtvw.DataSource = dv
dtvw.DataBind()
Return dtvw
End Function

Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Create DetailsView Dynamically based on Checkboxes</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="GridView1" runat="server">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="CheckBox1" runat="server" />
</itemtemplate>
</asp:templatefield>
</columns>

</asp:gridview>
<br />
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="OK" /><br />
<br />
&nbsp;<asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder>

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


momo said:
Guys,

I am trying to created a detailview that displays several records at a
time.
Let's say I have a GridView with checkboxes and when I check two
checkboxes
and click a button, I want it to give me two DetailViews; one for each
record selected. how can I do this? Or is there a better way than this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top