2011-08-14 6 views
0

이 코드를 작성했지만 해당 특성을 태그에 추가하지 못했습니다. 문제가 무엇입니까? 감사데이터 목록을 강조하는 데 문제가 있음

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

답변

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

+1; 그것은 절대적인 방법입니다. –

관련 문제