<%@ Import Namespace = "System.Data"%> <%@ Import Namespace = "System.Data.OleDb"%> <%@ Page Language="VB" Debug="true" ContentType="text/html" ResponseEncoding="utf-8" %> <SCRIPT language="VB" runat="server"> ' Rows property to hold the Rows in the ViewState Protected Property Rows() As Integer Get If Not ViewState("Rows") Is Nothing Then Return CInt(Fix(ViewState("Rows"))) Else Return 0 End If End Get Set(ByVal value As Integer) ViewState("Rows") = value End Set End Property ' Columns property to hold the Columns in the ViewState Protected Property Columns() As Integer Get If Not ViewState("Columns") Is Nothing Then Return CInt(Fix(ViewState("Columns"))) Else Return 0 End If End Get Set(ByVal value As Integer) ViewState("Columns") = value End Set End Property Sub listNow(ByVal sender As Object, ByVal e As EventArgs) Dim titleSQL = "Select blogTitle,blogger from tblBlogs" Dim con As New OleDbConnection("PROVIDER =Microsoft.ACE.OleDb.12.0; data source= " & server.MapPath("/mydatabase.accdb")) Dim adap As New OleDbDataAdapter(titleSQL, con) Dim ds1 As New Dataset() Dim vTable As DataTable Dim cmd =New OledbCommandBuilder(adap) con.Open adap.Fill(ds1,"pangLoad") con.Close() ' Run only once a postback has occured If Page.IsPostBack Then 'Set the Rows and Columns property with the value coming from the database Me.Rows = ds1.tables("pangLoad").Rows.Count Me.Columns = 1 End If Me.Rows = ds1.tables("pangLoad").Rows.Count Me.Columns = 1 CreateDynamicTable(titleSQL) End Sub Public Sub CreateDynamicTable(ByVal str as String) PlaceHolder1.Controls.Clear() 'Load data from the database Dim con As New OleDbConnection("PROVIDER =Microsoft.ACE.OleDb.12.0; data source= " & server.MapPath("/joan/AIMDbase.accdb")) Dim adap As New OleDbDataAdapter(str, con) Dim ds1 As New Dataset() Dim cmd =New OledbCommandBuilder(adap) con.Open adap.Fill(ds1,"pangLoad") con.Close() ' Fetch the number of Rows and Columns for the table ' using the properties Dim tblRows As Integer = Rows Dim tblCols As Integer = Columns ' Create a Table and set its properties Dim tbl As Table = New Table() ' Add the table to the placeholder control PlaceHolder1.Controls.Add(tbl) ' Now iterate through the table and add your controls For i As Integer = 0 To tblRows - 1 Dim tr As TableRow = New TableRow() For j As Integer = 0 To tblCols - 1 Dim tc As TableCell = New TableCell() Dim lblQs As LinkButton = New LinkButton() lblQs.Text = ds1.Tables("pangLoad").Rows(i).Item("blogTitle") lblQs.OnClick="ListContent" ' Add the control to the TableCell tc.Controls.Add(lblQs) ' Add the TableCell to the TableRow tr.Cells.Add(tc) Next j tbl.Rows.Add(tr) Next i ' This parameter helps determine in the LoadViewState event, ' whether to recreate the dynamic controls or not ViewState("dynamictable") =true End Sub ' Check the ViewState flag to determine whether to ' rebuild your table again Protected Overrides Sub LoadViewState(ByVal earlierState As Object) MyBase.LoadViewState(earlierState) If ViewState("dynamictable") Is Nothing Then CreateDynamicTable("") End If End Sub </script>
<table width="1037" border="0"> <tr> <td width="263" bgcolor="#FF9900"><strong>BLOGS</strong></td> <td width="764"> </td> </tr> <tr> <td bgcolor="#FF9900"><asp:PlaceHolder ID="PlaceHolder1" runat="server" OnLoad="listNow" ></asp:PlaceHolder></td> <td width="764"> </td> </tr> </table>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)