BaseDataList.DataBind BaseDataList.DataBind BaseDataList.DataBind BaseDataList.DataBind Method

Definizione

Associa il controllo e tutti i relativi controlli figlio all'origine dati specificata.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 ()

Esempi

Nell'esempio di codice riportato di seguito viene illustrato DataBind come utilizzare il metodo per associare un'origine DataGrid dati a un controllo.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>

Commenti

Utilizzare il DataBind metodo per associare l'origine dati specificata DataSource dalla proprietà al controllo elenco dati.Use the DataBind method to bind the data source specified by the DataSource property to the data listing control. Associando l'origine dati a un controllo elenco dati, le informazioni nell'origine dati vengono visualizzate in un controllo elenco dati.By binding the data source to a data listing control, the information in the data source is displayed in a data listing control.

Il DataBind metodo viene utilizzato comunemente anche per sincronizzare l'origine dati e un controllo elenco dati dopo l'aggiornamento delle informazioni nell'origine dati.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. In questo modo è possibile aggiornare anche tutte le modifiche nell'origine dati in un controllo elenco dati.This allows any changes in the data source to also be updated in a data listing control.

Se l'origine dati per il controllo elenco dati viene specificata dalla DataSourceID proprietà, non è necessario chiamare il DataBind metodo.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 chiama automaticamente questo metodo per associare il controllo origine dati specificato al controllo elenco dati.ASP.NET calls this method automatically to bind the specified data source control to the data listing control.

Si applica a

Vedi anche