Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Considering to use placeholder to display data from the database

Status
Not open for further replies.

cesark

Programmer
Dec 20, 2003
621
Hi there,

I am designing the page in which the details of an ‘Offer’ (after clicking in some Offer line) will be displayed. This page will execute a query to the database and will contain tables of data that sometimes, some of them, will not appear, and the same with some of the tables rows.

My question is: Which is the best control to use so that when some parts of the page are not, the rest of the parts are displayed normally without blank spaces between them? I mean hiding the parts where there are not data.

I am thinking in PlaceHolder, Is this the best control to use when rendering ‘variable’ data in a page?

Thank you,
Cesar
 
Personally I would create the controls at runtime as then you can control if they are only created and shown based on whether data exists for them. Using this approach means that which controls you use is irrelevent (although you can place them all in a panel if you want to keep them grouped together).

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
The question is: hiding whole tables, table rows and some controls. So, to hide a control I have set its visibility to false, to hide a table I have to use a panel. And, How I have to hide a table row?
 
Don't render that table row.

Rather than using CSS to "hide" controls that you don't want displayed, just don't render those controls in the first place.

If this "variable data" doesn't take the form of controls, then you can place LiteralControls into your PlaceHolder.

PlaceHolder1.Controls.Add(new LiteralControl("Some Text from database.<br>");

That way you systematically render only the appropriate HTML for a give result set.

You can render actual server controls as well, but only if you're very proficient on the issues involving dynamically created controls.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Hi,

When you say
Don't render that table row.
I suppose you mean don' t include that table row in the Panel. But I don' t know how to do it, in fact I don' t know how to create a table in a Panel and then hide or don' t include some rows depending on the data received from the DB. Would you mind giving a simple example please?


Thank you,
Cesar
 
OK here's a really basic example of creating a table at runtime and adding it to a panel:
Code:
    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 t As System.Web.UI.WebControls.Table
        Dim tRow As TableRow
        Dim tCell As TableCell
        Dim i As Integer

        ' Create a new table
        t = New System.Web.UI.WebControls.Table

        ' Loop through rows of data
        For i = 0 To 10
            ' If the number is even
            If i Mod 2 = 0 Then
                ' Create a new table row
                tRow = New TableRow
                ' Create a new table cell
                tCell = New TableCell
                tCell.Text = i
                ' Add the cell
                tRow.Cells.Add(tCell)
                ' Add the row
                t.Rows.Add(tRow)
            End If
        Next
        ' Add the table
        Panel1.Controls.Add(t)
    End Sub
All you would have to do is modify the code to loop through your data and add a row/cell when data exists (I've just added one when the variable is an even number).

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
And what about using a Panel with a table in it, and then setting the rows to (runat="server") so that in server code decide if render or not the rows?

For example:
Code:
<%@ Page Language="VB" Debug="true" %>

<script language="VB" runat="server">

Sub Page_Load(sender As Object, e As EventArgs)

 ' First retrieve the data from de database
  ...

 ' Hide the whole Panel
 If CmdOffer.Parameters("@SomeData").Value = "0" Then
  [b]pnlInfo.Visible = False[/b]
 End If

 ' Hide row1
 If CmdOffer.Parameters("@FullName").Value = "" Then
  [b]row1.Visible = False[/b]
 End If

 ' Hide only a server control
 If CmdOffer.Parameters("@Email").Value = "" Then
  [b]lblEmail.Visible = False[/b]
 End If

 ' Hide row2
 If CmdOffer.Parameters("@Phone").Value = "" Then
  [b]row2.Visible = False[/b]
 End If

 ' Hide row3
 If CmdOffer.Parameters("@BirthDate").Value = "" Then
  [b]row3.Visible = False[/b]
 End If

End Sub
</script>



<html>
<head>
<title>Test</title>
</head>
<body>
<form id="results" runat="server">
<asp:Panel ID="pnlInfo" Runat="server" Width="422px" Height="63px">
  <br />
  <table width="100%">
    [b]<tr id="row1" runat="server">[/b]
      <td>
        <asp:Label ID="lblFullName" Runat="server"  Text="Full name unknown">
        </asp:Label></td>
      <td><asp:Label ID="lblEmail" Runat="server">
        </asp:Label></td>
      [b]</tr>[/b]
    [b]<tr id="row2" runat="server">[/b]
      <td colspan="2">
        <asp:Label ID="lblPhone" Runat="server" Text="Phone number unknown">
        </asp:Label>
      </td>
    [b]</tr>[/b]
    [b]<tr id="row3" runat="server">[/b]
      <td colspan="2">
        <asp:Label ID="lblBirthDate" Runat="server"  Text="Birthdate  unknown">
        </asp:Label>
      </td>
    [b]</tr>[/b]
  </table>
 </asp:Panel>
</form>
</body>
</html>
 
That should work although you would have to add a reference to it on the server so that it knows it exists. e.g.
Code:
Protected WithEvents row1 As System.Web.UI.WebControls.TableRow

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Actually it may have to be declared as a HTML generic control. e.g.
Code:
Protected WithEvents row1 As System.Web.UI.HtmlControls.HtmlGenericControl

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Ok. But I don' t need to declare the row1 as an Html control since my code is 'in line', all is written in the same aspx page, I don' t use practically code behind, only in some occasions.
I tested it and works without the declaration you posted.

Thanks
 
You haven't told us if you want to render ASP.NET Server Controls into the page, or just display a table.

If you're displaying a simple table of information, build the table, server side, by placing LiteralControls into the PlaceHolder:

PlaceHolder1.Controls.Add(new LiteralControl("<table>"));

Do the same for your rows and cells. You can do this inside of a loop that is iterating a, for example, SqlDataReader. That way you can control exactly the contents of the table.

If a record in the datareader isn't relevant, skip it. Render the next one.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top