473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

asp:Table DataBind method?

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
Nov 18 '05 #1
3 11985
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
"Marty McDonald" <mc******@wsdot.wa.gov> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 18 '05 #2
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(connectionStri ng)

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...WebUIControlCl
assDataBindTopic.asp

Control.OnDataBinding Method
http://msdn.microsoft.com/library/en...WebUIControlCl
assOnDataBindingTopic.asp

Control.DataBinding Event
http://msdn.microsoft.com/library/en...WebUIControlCl
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" <mc******@wsdot.wa.gov>
From: "Marty McDonald" <mc******@wsdot.wa.gov>
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: <#N**************@TK2MSFTNGP10.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 164.110.202.164
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.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


Nov 18 '05 #3
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(connectionStri ng)

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...WebUIControlCl
assDataBindTopic.asp

Control.OnDataBinding Method
http://msdn.microsoft.com/library/en...WebUIControlCl
assOnDataBindingTopic.asp

Control.DataBinding Event
http://msdn.microsoft.com/library/en...WebUIControlCl
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" <mc******@wsdot.wa.gov>
From: "Marty McDonald" <mc******@wsdot.wa.gov>
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: <#N**************@TK2MSFTNGP10.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 164.110.202.164
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.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


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Greg Linwood | last post by:
I am wondering what the best approach to binding XML data to an asp:Table from the Page_Load event in a code behind module? I'm using VB.Net and initially approached this by adding a table to the...
2
by: Kevin | last post by:
I am just learning asp.net and ran into a problem that I have not been able to resolve. I have a web form with an html table that houses an asp:label, asp:textbox and asp:button within. I had the...
6
by: coleenholley | last post by:
I have a Class Module written in VB that calls an RPC (written in COBOL) we have written code to read the data from the RPC, and we create a temporary datatable to store the data from the RPC. This...
4
by: stb | last post by:
I have an empty asp:table on a form. Rows and cells in the rows are added programatically. At the end of each row, there is a cell with a button inside it. How do I catch the button's click...
8
by: tatemononai | last post by:
I had a beautiful script that was running, well, just beautifully. But then I decided to take a button that fired an event and place it inside a <asp:table. The event WILL NOT FIRE INSIDE THE...
0
by: Davey P | last post by:
I have a asp table which I am populating dynamically server side with various other controls such as text boxes, drop down lists and buttons. The controls in this table are filled with data stored...
0
by: Davey P | last post by:
I have an asp.net table which I am populating with controls dynamically at runtime. The controls themselves get/set values in a datatable which is stored in viewstate over postbacks. Here is...
2
by: clickon | last post by:
I am confused about the way in which asp:table objects work. When a control is within an asp table it generally appears to be in the scope of the tables parent control. E.g. if i have a page that...
3
by: Andy | last post by:
Hi folks, I have a customvalidator control that works properly if it isn't contained in an ASP:TABLE. But, when I place it inside an ASP:TABLE, I find that _ServerValidate doesn't get fired,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.