我已编写此代码,但该属性未能添加到标记中。问题是什么?感谢
protected void Page_Load(object sender, EventArgs e)
{
PycDBDataContext db = new PycDBDataContext();
IEnumerable<seller_profile> profs = from rows in db.seller_profiles select rows;
ProfilesView.DataSource = profs;
ProfilesView.ItemCreated += new DataListItemEventHandler(ProfilesView_ItemCreated);
ProfilesView.DataBind();
}
void ProfilesView_ItemCreated(object sender, DataListItemEventArgs e)
{
e.Item.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightblue';");
}
答案 0 :(得分:3)
您真正想要的是ItemDataBound
事件,而不是ItemCreated
事件。
像这样重写,你会没事的。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataList ProfilesView;
PycDBDataContext db = new PycDBDataContext();
IEnumerable<seller_profile> profs = from rows in db.seller_profiles select rows;
ProfilesView.DataSource = profs;
ProfilesView.ItemDataBound += new DataListItemEventHandler(ProfilesView_ItemDataBound);
ProfilesView.DataBind();
}
}
private void ProfilesView_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor = 'lightblue';");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor = 'white';");
}
}