我对ListView的工作方式有点迷茫。
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
DataRow myRow;
DataRowView myRowView;
myRowView = (DataRowView)e.Item.DataItem;
myRow = myRowView.Row;
HtmlTableRow myTR = (HtmlTableRow)e.Item.FindControl("trRow");
HtmlTableCell myTC = (HtmlTableCell)e.Item.FindControl("tdCell");
Label myCB = (Label)e.Item.FindControl("ResultLabel1");
if (myRow["Result"].ToString().Equals("true"))
myTC.Style.Value = "background-color:#00FF00;color: #000000;";
else
myTC.Style.Value = "background-color:#FF0000;color: #000000;";
}
ListView有一个寻呼机,这会导致对ItemCreated的回发,我无法再次访问dataRow。
有没有什么建议可以帮我处理回发邮件?
更新:
我将代码更改为读取名为ResultLabel1的标签的实际值,该值为True/False,但它返回一个空字符串。。我认为这比从数据集中读取结果要好。有人能看出哪里不对吗?
代码:
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
HtmlTableRow myTR = (HtmlTableRow)e.Item.FindControl("trRow");
HtmlTableCell myTC = (HtmlTableCell)e.Item.FindControl("tdCell");
// Retrieve the current item.
ListViewItem item = e.Item;
// Verify if the item is a data item.
if (item.ItemType == ListViewItemType.DataItem)
{
// Get the Label ResultLabel1 control in the item.
Label myCB = (Label)item.FindControl("ResultLabel1");
if (myCB.Text.Equals("True"))
myTC.Style.Value = "background-color:#00FF00;color: #000000;";
else
myTC.Style.Value = "background-color:#FF0000;color: #000000;";
}
}
<ItemTemplate>
<tr id="trRow" runat="server" style="">
<td>
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
</td>
<td>
<asp:Label ID="DateTime_StartLabel" runat="server"
Text='<%# Eval("DateTime_Start") %>' />
</td>
<td>
<asp:Label ID="DateTime_EndLabel" runat="server"
Text='<%# Eval("DateTime_End") %>' />
</td>
<td>
<asp:Label ID="TestCaseNameLabel" runat="server"
Text='<%# Eval("TestCaseName") %>' />
</td>
<td>
<asp:Label ID="ModelNameLabel" runat="server" Text='<%# Eval("ModelName") %>' />
</td>
<td id="tdCell" runat="server">
<asp:Label ID="ResultLabel1" runat="server"
Text='<%# Eval("Result") %>' />
</td>
</tr>
</ItemTemplate>