日期:2010-08-04  浏览次数:20485 次

  本例用代码实现了创建DataGrid并响应了CheckBox事件,并实现了超链接列的多变量传送。

  创建一个前台页面

CreateDataGrid.aspx:

  在Form中添加一个PlcaeHolder,ID为"ph",Runat="Server"

  后台页面

CreateDatagrid.aspx.cs

    public class CreateDataGrid : System.Web.UI.Page
    {
        public String sql = "Select FirstName,LastName,HomePhone,Title FROM Employees";
        public DataGrid mygrid = new DataGrid();
        protected System.Web.UI.WebControls.PlaceHolder ph;
        public String SortExpression;
        private void Page_Load(object sender, System.EventArgs e)
        {
              //CreateDataGridForm.Controls.Add(MakeGrid());
           
            this.ph.Controls.Add(MakeGrid());
        }
//        protected override void CreateChildControls()
//        {
           
            //base.CreateChildControls ();
        //}

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {   
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
        /// <summary>
        /// 创建一个模板列和一个列模板
        /// </summary>

        public TemplateColumn tm = new TemplateColumn();
        public ColumnTemplate mycol = new ColumnTemplate();

        //返回DataView
        public DataView CreateDataSource ()
        {
            string strSql;
            strSql = "Data Source=localhost;Initial Catalog=Northwind;User Id=sa;Password=sa;";
            SqlConnection conn = new SqlConnection(strSql);
          &