BaseDataList.DataBind Método
Definição
Associa o controle e todos os seus controles filho à fonte de dados especificada.Binds the control and all its child controls to the specified data source.
public:
override void DataBind();
public override void DataBind ();
override this.DataBind : unit -> unit
Public Overrides Sub DataBind ()
Exemplos
O exemplo de código a seguir demonstra como usar o DataBind método para associar uma fonte de dados a um DataGrid controle.The following code example demonstrates how to use the DataBind method to bind a data source to a DataGrid control.
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script language="C#" runat="server">
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
// Load this data only once.
ItemsGrid.DataSource= CreateDataSource();
ItemsGrid.DataBind();
}
}
</script>
<head runat="server">
<title>DataGrid Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Example</h3>
<b>Product List</b>
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="true"
runat="server">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
</asp:DataGrid>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script language="VB" runat="server">
Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 *(i + 1)
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
' Load this data only once.
ItemsGrid.DataSource = CreateDataSource()
ItemsGrid.DataBind()
End If
End Sub 'Page_Load
</script>
<head runat="server">
<title>DataGrid Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Example</h3>
<b>Product List</b>
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="true"
runat="server">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
</asp:DataGrid>
</form>
</body>
</html>
Comentários
Use o DataBind método para associar a fonte de dados especificada pela DataSource propriedade ao controle de listagem de dados.Use the DataBind method to bind the data source specified by the DataSource property to the data listing control. Ao vincular a fonte de dados a um controle de listagem de dados, as informações na fonte de dados são exibidas em um controle de listagem de dados.By binding the data source to a data listing control, the information in the data source is displayed in a data listing control.
O DataBind método também é usado normalmente para sincronizar a fonte de dados e um controle de listagem de dados depois que as informações na fonte de dados são atualizadas.The DataBind method is also commonly used to synchronize the data source and a data listing control after information in the data source is updated. Isso permite que todas as alterações na fonte de dados também sejam atualizadas em um controle de listagem de dados.This allows any changes in the data source to also be updated in a data listing control.
Se a fonte de dados do controle de listagem de dados for especificada pela DataSourceID propriedade, você não precisará chamar o DataBind método.If the data source for the data listing control is specified by the DataSourceID property, you do not need to call the DataBind method. ASP.NET chama esse método automaticamente para associar o controle de fonte de dados especificado ao controle de listagem de dados.ASP.NET calls this method automatically to bind the specified data source control to the data listing control.