Active Server Pages Programming (ASP)/Can we create Repeater on DataGrid
Expert: Srini Nagarajan - 2/7/2005
Questionhi all,
can we create Repeater on DataGrid? how?
If we can then just send me sample code.
Tushar Dabhi
AnswerHi
Here is the sample script. Change the Database call and other stuff based on your requirement
Happy Programming!
-Srini
--Save this as 'Report1.aspx'---------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Report1.aspx.vb" Inherits="VB.Report1"%>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>nested</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form2" method="post" runat="server">
<asp:repeater id="parentRepeater" runat="server">
<HeaderTemplate>
<table border="1" width="300">
<tr>
<td width="100"><b>Size</b></td>
<td width="100"><b>Stock</b></td>
<td width="100"><b>Price</b></td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table border="1" width="300">
<tr>
<td width="100"><b><%# DataBinder.Eval(Container.DataItem,"Brand") %></b></td>
<td width="200" align="center"><b><%# DataBinder.Eval(Container.DataItem,"Code") %></b></td>
</tr>
</table>
<asp:repeater id="childRepeater" datasource='<%# CType(Container.DataItem,DataRowView).CreateChildView("OrderRelation") %>' runat="server">
<ItemTemplate>
<table border="1" width="300">
<tr>
<td width="100"><%# DataBinder.Eval(Container.DataItem, "[size]")%></td>
<td width="100"><%#CheckStock(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "[stock]")))%></td>
<td width="100"><%# DataBinder.Eval(Container.DataItem, "[price]")%></td>
</tr>
</table>
</ItemTemplate>
</asp:repeater>
</td> </tr>
</ItemTemplate>
</asp:repeater>
</form>
</body>
</HTML>
--save this as 'WebForm1.aspx.vb' --------
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class Report1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents parentRepeater As System.Web.UI.WebControls.Repeater
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim cnn As New SqlConnection("server=INSIGHT;UID=sa;PWD=Insight;database=Pubs;")
Dim ds As New DataSet
cnn.Open()
Dim cmd1 As New SqlDataAdapter("select * from product", cnn)
Dim cmd2 As New SqlDataAdapter("select * from stock", cnn)
cmd1.Fill(ds, "product")
cmd2.Fill(ds, "stock")
ds.Tables(0).TableName = "product"
ds.Tables(1).TableName = "stock"
Dim Parent As DataColumn = ds.Tables("product").Columns("prod_id")
Dim Child As DataColumn = ds.Tables("stock").Columns("prod_id")
Dim ORelation As DataRelation = New DataRelation("OrderRelation", Parent, Child, False)
ds.Relations.Add(ORelation)
parentRepeater.DataSource = ds.Tables("product")
DataBind()
cnn.Close()
End Sub
' This function will take the stock and
' retrun you the label if there is Stock
' it will print Stock other wise it will
' print 'No Stock'
Protected Function CheckStock(ByVal Stock) As String
If (Stock > 0) Then
Return Stock.ToString()
Else
Return "No Stock"
End If
End Function
End Class