嘿王牌大賤諜這裏是代碼
ASPX CODE:
<asp:ListView ID="ListView1" runat="server"
onitemdatabound="ListView1_ItemDataBound">
<LayoutTemplate>
<table id="itemPlaceholderContainer" border="0" cellpadding="0" cellspacing="0">
<tr>
<th>
EmpID
</th>
<th>
EmpName
</th>
<th>
Department
</th>
<th>
Age
</th>
<th>
Address
</th>
</tr>
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="MainTableRow" runat="server">
<td id="EmpID" runat="server">
<asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>' />
</td>
<td id="EmpNameTD" runat="server">
<asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' />
</td>
<td id="DepartmentTD" runat="server">
<asp:Label ID="DepartmentLabel" runat="server" Text='<%# Eval("Department") %>' />
</td>
<td id="AgeTD" runat="server">
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
</td>
<td id="AddressTD" runat="server">
<asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
,這裏是代碼隱藏代碼
protected void Page_Load(object sender, EventArgs e)
{
var Employee = new { EmpID = 1, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" };
var customerList = (new[] { Employee }).ToList();
customerList.Add(new { EmpID = 2, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 3, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 4, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 5, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 6, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 7, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 8, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 9, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 10, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" });
ListView1.DataSource = customerList;
ListView1.DataBind();
}
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataitem = (ListViewDataItem)e.Item;
int policyid = (int)DataBinder.Eval(dataitem.DataItem, "EmpID");
if (policyid == 3)
{
HtmlTableRow cell = (HtmlTableRow)e.Item.FindControl("MainTableRow");
cell.BgColor = "Red";
}
}
}
感謝