突出数据列表的问题

本文关键字:问题 列表 数据 | 更新日期: 2023-09-27 18:05:27

我已经编写了这段代码,但是无法将属性添加到标记中。有什么问题吗?由于

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';");
}

突出数据列表的问题

您真正想要的是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';");
    }
}