GridView 公共构造函数 利用PlaceHolder可以动态添加一个gridview

在表中显示数据源的值,其中每列表示一个字段,每行表示一条记录。GridView 控件允许您选择和编辑这些项以及对它们进行排序。

 公共构造函数

  名称 说明
Public method GridView 初始化 GridView 类的新实例。

 

C#
public GridView ()

 

使用此构造函数初始化 GridView 类的新实例。若要将 GridView 控件动态添加到页中,请新建一个 GridView 对象,设置它的属性,然后将它添加到某容器控件(如 PlaceHolder)的 Control.Controls 集合中。

 

下面的代码示例演示如何使用构造函数在页面上动态添加 GridView 控件。

 

<%@ Page language="C#" %>

<script runat="server">

    void Page_Load(Object sender, EventArgs e)
    {
       
        // Create a new GridView object.
        GridView customersGridView = new GridView();
         
        // Set the GridView object's properties.
        customersGridView.ID = "CustomersGridView";
        customersGridView.DataSourceID = "CustomersSource";
        customersGridView.AutoGenerateColumns = true;
        
        // Add the GridView object to the Controls collection
        // of the PlaceHolder control.
        GridViewPlaceHolder.Controls.Add(customersGridView);
        
    }

</script>

<html>
  <body>
    <form runat="server">

      <h3>GridView Constructor Example</h3>

      <asp:placeholder id="GridViewPlaceHolder"
        runat="Server"/>
    

  <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server"/>
   

   </form>
  </body>
</html>

 

 

posted @ 2009-04-16 15:51  minmin8110  阅读(213)  评论(0)    收藏  举报