asp:Table DataBind method?

M

Marty McDonald

I have <asp:Table... </asp:Table> on my webform. In codebehind, I populate
a DataTable whose data should appear in the asp:Table. I created my own
code to populate the asp:Table with the DataTable, then I discovered the
asp:Table has a DataBind method. But the method takes no args and so I'm
confused how to use it. Is there a link to see how DataBind works for
asp:Table? Should I just use my own code anyway? Thanks...

Here's my code in case it helps answer any questions...
/// <summary>
/// Creates a Web Control Table from a DataTable.
/// </summary>
/// <param name="dataTable">DataTable containing data.</param>
/// <returns>Web Control Table with data from the DataTable.</returns>
public static System.Web.UI.WebControls.Table Map(System.Data.DataTable
dataTable)
{
System.Web.UI.WebControls.Table webTable = new
System.Web.UI.WebControls.Table();

foreach(System.Data.DataRow dataRow in dataTable.Rows)
{
System.Web.UI.WebControls.TableRow webRow = new
System.Web.UI.WebControls.TableRow();
foreach(System.Data.DataColumn dataColumn in dataTable.Columns)
{
System.Web.UI.WebControls.TableCell webCell = new
System.Web.UI.WebControls.TableCell();
Label label = new Label();
string colname = dataColumn.ColumnName;
label.Text = dataRow[colname].ToString();
webCell.Controls.Add(label);
webRow.Cells.Add(webCell);
}
webTable.Rows.Add(webRow);
}
return webTable;

}//end method
 
H

Hermit Dave

y are you using table control to bind to a datatable... instead if you are
concerned bout it being lighter.. consider using repeater control.

--
Regards,

HD

Once a Geek.... Always a Geek
 
M

Mike Moore [MSFT]

Hi Marty,

How do you use the DataBind method to bind a Table to a data source when it
doesn't have a DataSource property? As you mentioned, the DataBind method
doesn't have any arguments.

The DataBind method initiates a series of functions to get the work done.
Among other things, DataBind calls OnDataBinding which triggers the
control's DataBinding event. Though the table control does not have a
DataSource property, it does have a DataBinding event. You can use this
event to programmatically populate the table.

To phrase this another way, the table control does not have the ability to
populate itself from a data source. You have to populate it
programmatically. One way to do this is to call the table's DataBind method
and put code in the table's DataBinding event to populate the control. Here
is a sample.


*** ASPX
<asp:Table id="Table2" runat="server"></asp:Table>

*** Code-behind
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
Table2.DataBind()
End If
End Sub

Private Sub Table2_DataBinding(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Table2.DataBinding

Dim Qry1 As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = _
"server='localhost'; trusted_connection=true; Database='pubs'"

Dim sqlConnection As System.Data.SqlClient.SqlConnection = _
New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT au_lname FROM authors"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = _
New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)

sqlConnection.Open()
Qry1 = sqlCommand.ExecuteReader()
CreateRows(sender, Qry1)
Qry1.Close()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
End Sub

Private Sub CreateRows(ByVal t As Table, _
ByVal Qry1 As System.Data.SqlClient.SqlDataReader)

Dim r As TableRow
Dim c As TableCell
While Qry1.Read()
r = New TableRow
c = New TableCell
c.Text = Qry1.GetString(0)
r.Cells.Add(c)
t.Rows.Add(r)
End While
End Sub


---
Here's where you can find documentation on DataBind, OnDataBinding and
DataBinding. The road map article provides information on the internals of
data binding.

Control.DataBind Method
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIControlCl
assDataBindTopic.asp

Control.OnDataBinding Method
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIControlCl
assOnDataBindingTopic.asp

Control.DataBinding Event
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIControlCl
assDataBindingTopic.asp

INFO: Roadmap for Web Forms Data Binding
http://support.microsoft.com/default.aspx?kbid=313481

---
Does this answer your question?

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.

This posting is provided "AS IS", with no warranties, and confers no rights.

--------------------
Reply-To: "Marty McDonald" <[email protected]>
From: "Marty McDonald" <[email protected]>
Subject: asp:Table DataBind method?
Date: Wed, 14 Jan 2004 10:55:36 -0800
Lines: 40
Organization: WSDOT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 164.110.202.164
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
..phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:202179
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

I have <asp:Table... </asp:Table> on my webform. In codebehind, I populate
a DataTable whose data should appear in the asp:Table. I created my own
code to populate the asp:Table with the DataTable, then I discovered the
asp:Table has a DataBind method. But the method takes no args and so I'm
confused how to use it. Is there a link to see how DataBind works for
asp:Table? Should I just use my own code anyway? Thanks...

Here's my code in case it helps answer any questions...
/// <summary>
/// Creates a Web Control Table from a DataTable.
/// </summary>
/// <param name="dataTable">DataTable containing data.</param>
/// <returns>Web Control Table with data from the DataTable.</returns>
public static System.Web.UI.WebControls.Table Map(System.Data.DataTable
dataTable)
{
System.Web.UI.WebControls.Table webTable = new
System.Web.UI.WebControls.Table();

foreach(System.Data.DataRow dataRow in dataTable.Rows)
{
System.Web.UI.WebControls.TableRow webRow = new
System.Web.UI.WebControls.TableRow();
foreach(System.Data.DataColumn dataColumn in dataTable.Columns)
{
System.Web.UI.WebControls.TableCell webCell = new
System.Web.UI.WebControls.TableCell();
Label label = new Label();
string colname = dataColumn.ColumnName;
label.Text = dataRow[colname].ToString();
webCell.Controls.Add(label);
webRow.Cells.Add(webCell);
}
webTable.Rows.Add(webRow);
}
return webTable;

}//end method
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top