1

Edit for vhinn

enter image description here

I want it to look like this:

enter image description here

I am trying to build an html table dynamically on pageload with variables from a database.

this is an example strictly html http://jsfiddle.net/jdv590/daCum/1/

code:

    Private Sub brothersgird()
        Dim html As New StringBuilder
        Dim sql As String = "select Name, Hometown, Picture, Class from brothers",
            connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~/App_Data/Members.accdb;Persist Security Info=False;",
            conn As New OleDbConnection(connstring),
         myCommand As New OleDbCommand(sql, conn),
         namevar As String,
        classvar As String,
        hometownvar As String
        Dim x As Integer = 1
        conn.Open()
        Dim dr As OleDbDataReader = myCommand.ExecuteReader
        html.Append("<table>")
        Do While dr.Read
            ' imagevar = dr("Picture")
            namevar = dr("Name")
            classvar = dr("Class")
            hometownvar = dr("Hometown")
            html.Append("<tr>")
            Do While x < 4
                html.Append("<td><p>" & namevar & "<br /> Hometown: " & hometownvar & "<br /> Class: " & classvar & "</p></td>")
                x = x + 1
            Loop
            html.Append("</tr>")
            x = 0
        Loop
        html.Append("</table>")
        dr.Close()
        conn.Close()
    End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        brothersgird()
'write to panel maybe with this idea:
seniorpanel.html=html ???

    End Sub

aspx side:

<asp:Panel ID="seniorpanel" runat="server">
    </asp:Panel>
1
  • 1
    Try using ListView. It's basically the same thing like Gridview, but on ListView you are freely to style the content unlike in Gridview, it will always be in Table Style. Based on your requirement, you need to design it using DIVs and not Table. Commented Nov 10, 2011 at 13:12

3 Answers 3

3

in your markup:

<asp:Panel ID="seniorpanel" runat="server">
    <asp:GridView ID="brothersgird" runat="server" ShowHeader="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <p>
                        <%# Eval("Name")%><br />
                        Hometown: 
                        <%# Eval("Hometown")%><br />
                        Class: 
                        <%# Eval("Hometown")%>
                    </p>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</asp:Panel>

code-behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        brothersgird.DataSource = SelectBrothers()
        brothersgird.DataBind()
    End If
End Sub

Private Function SelectBrothers() As DataTable
    Dim sql As String = "select Name, Hometown, Picture, Class from brothers"
    Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~/App_Data/Members.accdb;Persist Security Info=False;"
    Dim conn As New OleDbConnection(connstring)

    Dim ds As New DataSet
    Dim adapter As New OleDbDataAdapter()
    adapter.SelectCommand = New OleDbCommand(sql, conn)
    adapter.Fill(ds)

    Return ds.Tables(0)
End Function
Sign up to request clarification or add additional context in comments.

6 Comments

this is ALMOST exactly what I am looking for. The only thing is in addition to the template field I am still getting the data in separate fields. check the edit for a photo
@mumis2012 - There's a attribute on the grid, something like "AutoGenerateColumns" set that to false.
@Phill that did it. thanks. one more thing. I now have one long column with about 20 rows. I would like to have the grid be 3 columns across, then after 3 cells in a row start a new line. any idea how to do this?
@mumis2012 - you will need to use the ListView control which allows you to define columns. It's basically exactly the same as what you've done, except for a different purpose, which happens to cater for your scenario.
@Phill any idea how to display an image in the list view layout stored in the access database? aka something like: <ItemTemplate> <td> <img src="<%# Eval("picture")%>" alt="headshot" /><br /><br /> <br /> Hometown: <%# Eval("Hometown")%><br /> Class: <%# Eval("class")%> </td> </ItemTemplate>
|
1

You familiar with DataGrids?

Client Side:

<asp:DataGrid runat="server" id="dataTable">
</asp:DataGrid>

Server Side:

//Get your data table from the database - let's say the variable is called dt
dataTable.DataSource = dt
dataTable.DataBind()

There is a lot more you can do with datagrids, but this should be enough to get you started if you decide to go this route.

Comments

0

Are you asking how to do this in javascript if you already have the data? If so an example would be kinda like this:

var row = document.createElement("TR");
var th1 = document.createElement("TH");
row.appendChild(th1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.