1

我有一个DataList,当它不是回发时,我在页面加载中绑定它,但是当我尝试访问事件时,我仍然收到一个空引用异常,DataItemItemCreated什么建议吗?

protected void Page_Load(object sender, EventArgs e)
{
    AppPath = MapPath(HttpContext.Current.Request.ApplicationPath);
    MainDS.ReadXml(AppPath + FileName);
    DataView MyDV = new DataView(MainDS.Tables[0]);
    DataList1.DataSource = MyDV;
    DataList1.DataBind();           
}

protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{                  
    Response.Write(e.Item.DataItem.ToString());      
}
4

1 回答 1

1

您需要检查项目是普通项目,而不是页眉或页脚:

protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{                  
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Response.Write(e.Item.DataItem.ToString());      
    }
}
于 2013-08-04T08:25:09.513 回答