You could consider using a Template Item to format the display of your data....but a Table will still be rendered if you use a GridView.
You could create your own Templated Control instead of using an existing ASP.NET control if you want. Then you can specify exactly what should be placed where and how without using tables (if you wish).
For example, here I've created a new Web User Control (named TemplatedUserControl).
This User Control has 2 properties:
- one that accepts a template used to display the data,
- and one that accepts a template used to edit the data.
This control has a private class "DataItem" which inherits from the Control class and implements the INamingContainer interface. This private class is used in order to display dynamic data using a unique name in the templated control.
The Control dynamically displays the data (which you can retrieve from a database but here I'm just using an Array of Strings) by dynamically creating instances of the DataItem class which is applied to the template(s) provided (using the 2 template properties I described earlier):
-
Public Partial Class TemplatedUserControl
-
Inherits System.Web.UI.UserControl
-
Private _editing As Boolean = False
-
Private m_messageTemplate As ITemplate = Nothing
-
Private m_messageEditTemplate As ITemplate = Nothing
-
-
-
<TemplateContainer(GetType(DataItem))> _
-
Public Property DataTemplate() As ITemplate
-
Get
-
Return m_messageTemplate
-
End Get
-
Set(ByVal value As ITemplate)
-
m_messageTemplate = value
-
End Set
-
End Property
-
<TemplateContainer(GetType(DataItem))> _
-
Public Property EditTemplate() As ITemplate
-
Get
-
Return m_messageEditTemplate
-
End Get
-
Set(ByVal value As ITemplate)
-
m_messageEditTemplate = value
-
End Set
-
End Property
-
-
Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
-
-
Dim myData() As String = _
-
{"first record", "second record", "third record", "fourth record"}
-
Dim i As Integer
-
-
If DataTemplate IsNot Nothing AndAlso _editing = False Then
-
For i = 0 To myData.Length - 1
-
Dim container As New DataItem(i, myData(i))
-
DataTemplate.InstantiateIn(theItem)
-
Me.Controls.Add(theItem)
-
Next
-
ElseIf EditTemplate IsNot Nothing AndAlso _editing Then
-
For i = 0 To myData.Length - 1
-
Dim theItem As New DataItem(i, myData(i))
-
EditTemplate.InstantiateIn(theItem)
-
Me.Controls.Add(theItem)
-
Next
-
-
End If
-
End Sub
-
-
Public Class DataItem
-
Inherits Control
-
Implements INamingContainer
-
-
Private m_index As Integer
-
Private m_message As String
-
Friend Sub New(ByVal i As Integer, ByVal msg As String)
-
Me.Index = i
-
Me.Message = msg
-
End Sub
-
-
Public Property Index() As Integer
-
Get
-
Return m_index
-
End Get
-
Set(ByVal value As Integer)
-
m_index = value
-
End Set
-
End Property
-
-
Public Property Message() As String
-
Get
-
Return m_message
-
End Get
-
Set(ByVal value As String)
-
m_message = value
-
End Set
-
End Property
-
End Class
-
-
End Class
-
Ok, so now we have a Templated Control.
To use this you would create an ASP page and add the control to it:
- <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="ScratchPad.WebForm1" %>
-
-
<%@ Register TagPrefix="uc" TagName="TemplateTest" Src="~/TemplatedUserControl.ascx" %>
-
-
<asp:Panel runat="server" id="contents" style="border: none;">
-
<div style="width:150px;float:left; text-align:center; font-weight:bold; border-bottom:solid 1px black;">Index</div>
-
<div style="width:150px;float:left; text-align:center;font-weight:bold; border-bottom:solid 1px black;">Message</div>
-
<div style="clear:both"></div>
-
<uc:TemplateTest ID="myTemplatedCtrl" runat="server">
-
<datatemplate>
-
<div style="width:150px;float:left;text-align:center"><asp:Literal runat="server" ID="textValue" Text="<%# Container.Index %>"></asp:Literal></div>
-
<div style="width:150px;float:left;text-align:center"><asp:Literal runat="server" ID="indexValue" Text="<%# Container.Message %>"></asp:Literal></div>
-
<div style="clear:both"></div>
-
</datatemplate>
-
<edittemplate>
-
<div style="width:150px;float:left; text-align:center"><asp:TextBox runat="server" ID="Literal1" Text="<%# Container.Index %>"></asp:TextBox></div>
-
<div style="width:150px;float:left;text-align:center"><asp:TextBox runat="server" ID="Literal2" Text="<%# Container.Message %>"></asp:TextBox></div>
-
<div style="clear:both"></div>
-
</edittemplate>
-
</uc:TemplateTest>
-
</asp:panel>
Notice the Templates that I've supplied to the instance of the template control (the TemplateTest control). These Templates are passed into the 2 properties I described above.
If the Templated Control is configured such that it is not in edit mode the content will be displayed as text in <div> tags that have styles to make the text appear as if in a table layout....otherwise the text displayed in TextBoxes within the <div> tags. (I didn't add a property to let you specify whether or not the Template Control is in edit mode....just change the private _edit variable member in the Templated Control to change which template is used)
All you have to do in the ASP page is call the DataBind method to bind the data retrieved in the Templated control to the template provided in the page:
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
DataBind()
-
End Sub