protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            for (int i = 1; i <=pageNum; i++)
            {
                LinkButton lbtn = new LinkButton();
                lbtn.ID = "lbtn" + i.ToString();
                lbtn.CommandName = "lbtn" + i.ToString();
                lbtn.Text = i.ToString()+" ";
                lbtn.Font.Underline = false;
                e.Item.Controls.Add(lbtn);
            }
        }
    }
这是datalist添加页码的代码,与datagrid类似

解决方案 »

  1.   

    将Footer改为Header,LinkButton改为CheckBox等试试
      

  2.   

    在绑定DataGrid的时候动态创建
      

  3.   

    在绑定DataGrid的时候动态创建
      

  4.   

    这是一段我自己研究带checkbox功能DataGrid物件,给你参考一下
    public override void InitializeCell(TableCell cell, 
    int columnIndex, ListItemType itemType) 
    {
    //let the base class initialize the cell
    base.InitializeCell(cell, columnIndex, itemType);
    ViewState["_columnIndex"]=columnIndex;
    if(    itemType == ListItemType.EditItem || 
    itemType == ListItemType.Item || 
    itemType == ListItemType.AlternatingItem || 
    itemType == ListItemType.SelectedItem ||
    itemType == ListItemType.Header)
    {
    CheckBox checkbox = new CheckBox();
    //assign an ID that we can use to find the control later
    //we don't want to add a normal checkbox to the header.
    checkbox.ID = (itemType==ListItemType.Header)? ("checkboxHead"+"_"+columnIndex): ("checkboxCol"+"_"+columnIndex);
    if(checkbox.ID==("checkboxHead"+"_"+columnIndex))
    checkbox.Attributes.Add("onclick","CheckAll(this)");
    else if(checkbox.ID==("checkboxCol"+"_"+columnIndex))
    checkbox.Attributes.Add("onclick","CheckChanged(this)");
    cell.Controls.Add(checkbox);
    }
    }