Wednesday, March 23, 2011

Adding Attributes to ListView item in Asp.Net

One problem when switching from GridView to ListView would be to understand that ListView doesn't have rows as GridView. To add attributes to ListView item, it should be done in ItemDataBound event as below.

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
if (e.Item.ItemType == ListViewItemType.DataItem)
object objTemp = ListView1.DataKeys[((ListViewDataItem)e.Item).DisplayIndex].Value as object;
if (objTemp != null)
string id = objTemp.ToString();
((HtmlTableRow)e.Item.FindControl("courserow")).Attributes["onmouseover"] = "'DodgerBlue';'hand';";
((HtmlTableRow)e.Item.FindControl("courserow")).Attributes["onmouseout"] = "'Black';";
((HtmlTableRow)e.Item.FindControl("courserow")).Attributes["onclick"] = "window.location.href = 'ManageCourse.aspx?CourseId=" + id + "'";

You need the runtat attribute set to server for "courserow" row.

No comments:

Post a Comment