共用方式為


DetailsView 建構函式

定義

初始化 DetailsView 類別的新執行個體。

public:
 DetailsView();
public DetailsView ();
Public Sub New ()

範例

下列程式碼範例示範如何使用 建構函式,以動態方式將控制項新增 DetailsView 至頁面。

<!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" >

  <head>
    <title>DetailsView Constructor Example</title>
<script runat="server">
    
      void Page_Load(Object sender, EventArgs e)
      {
        
        // Create a new DetailsView object.
        DetailsView storeDetailsView = new DetailsView();
        
        // Set the DetailsView object's properties.
        storeDetailsView.ID = "StoresDetailsView";
        storeDetailsView.DataSourceID = "StoresSqlDataSource";
        storeDetailsView.AutoGenerateRows = true;
        storeDetailsView.AllowPaging = true;
        storeDetailsView.PagerSettings.Mode = 
          PagerButtons.NextPrevious;

        // Add the DetailsView object to the Controls collection
        // of the PlaceHolder control.
        DetailsViewPlaceHolder.Controls.Add(storeDetailsView);

      }
    
    </script>
  
  </head>
  
  <body>
    <form id="form1" runat="server">
        
      <h3>DetailsView Constructor Example</h3>
        
        <!-- Use a PlaceHolder control as the container for the -->
        <!-- dynamically generated DetailsView control.         -->       
        <asp:PlaceHolder id="DetailsViewPlaceHolder"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects -->
        <!-- to the Pubs sample database.                        -->  
        <asp:sqldatasource id="StoresSqlDataSource"  
          selectcommand="SELECT [stor_id], [stor_name], [stor_address], 
            [city], [state], [zip] FROM [stores]"
          connectionstring=
            "server=localhost;database=pubs;integrated security=SSPI"
          runat="server">          
        </asp:sqldatasource>
            
      </form>
  </body>
</html>
<!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" >

  <head>
    <title>DetailsView Constructor Example</title>
<script runat="server">
    
      Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        
        ' Create a new DetailsView object.
        Dim storeDetailsView As New DetailsView()
        
        ' Set the DetailsView object's properties.
        storeDetailsView.ID = "StoresDetailsView"
        storeDetailsView.DataSourceID = "StoresSqlDataSource"
        storeDetailsView.AutoGenerateRows = True
        storeDetailsView.AllowPaging = True
        storeDetailsView.PagerSettings.Mode = _
          PagerButtons.NextPrevious

        ' Add the DetailsView object to the Controls collection
        ' of the PlaceHolder control.
        DetailsViewPlaceHolder.Controls.Add(storeDetailsView)

      End Sub
    
    </script>
  
  </head>
  
  <body>
    <form id="form1" runat="server">
        
      <h3>DetailsView Constructor Example</h3>
        
        <!-- Use a PlaceHolder control as the container for the -->
        <!-- dynamically generated DetailsView control.         -->       
        <asp:PlaceHolder id="DetailsViewPlaceHolder"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects -->
        <!-- to the Pubs sample database.                        -->  
        <asp:sqldatasource id="StoresSqlDataSource"  
          selectcommand="SELECT [stor_id], [stor_name], [stor_address], 
            [city], [state], [zip] FROM [stores]"
          connectionstring=
            "server=localhost;database=pubs;integrated security=SSPI"
          runat="server">          
        </asp:sqldatasource>
            
      </form>
  </body>
</html>

備註

使用此建構函式來初始化 類別的新實例 DetailsView 。 若要動態將控制項新增 DetailsView 至頁面,請建立新的 DetailsView 物件、設定其屬性,然後將它新增至 Control.Controls 容器控制項的集合,例如 PlaceHolder

適用於

另請參閱