我在管理员方面显示有关客户的某些信息,我显示的其中一个细节是“EventStatus”。现在 EventStatus 有不同的值,如“已接受”,“已拒绝”,“待定”,“正在进行中”等。
现在我从数据库动态加载此信息。
我想要做的是 - 根据它所拥有的'价值'来改变这个标签的CSS。
我的aspx代码是:
<tbody>
<asp:Repeater ID="rptr" runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("CustomerName") %></td>
<td><%# Eval("Name") %></td>
<td><%# Eval("EventType") %></td>
<td><%# Eval("EventDate", "{0:dd-MM-yyyy}") %></td>
<td><asp:Label ID="lblStatus" runat="server" CssClass="label label-primary" Text='<%# Eval("EventStatus") %>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
Page_Load上的C#代码:
if (!IsPostBack)
{
EventLogic eventLogic = new EventLogic();
rptr.DataSource = eventLogic.pendingOrders(Convert.ToInt32(Request.QueryString["ID"]));
rptr.DataBind();
}
我添加了这个ItemDataBound_Event:
protected void rptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Event eventItem = (Event)e.Item.DataItem;
Label lblStatus = (Label)e.Item.FindControl("lblStatus ");
switch (eventItem.EventStatus)
{
case "Accepted":
lblStatus.CssClass = "label label-primary";
break;
case "Rejected":
lblStatus.CssClass = "label label-rejected";
break;
case "Pending":
lblStatus.CssClass = "label label-info";
break;
case "In Progress":
lblStatus.CssClass = "label label-warning";
break;
case "Completed":
lblStatus.CssClass = "label label-success";
break;
}
}
}
但我得到一个错误:无法将“System.Data.DataRowView”类型的对象强制转换为“BusinessLogic.Event”。
答案 0 :(得分:4)
我会使用ItemDataBound事件,而不是依赖Eval
。
标记:
<asp:Repeater ID="rptr" runat="server" OnItemDataBound="rptr_ItemDataBound">
<ItemTemplate>
<tr>
<td><asp:Literal ID="litCustomerName" runat="server" /></td>
<td><asp:Literal ID="litName" runat="server" /></td>
<td><asp:Literal ID="litEventType" runat="server" /></td>
<td><asp:Literal ID="litEventDate" runat="server" /></td>
<td><asp:Label ID="lblStatus" runat="server" CssClass="label label-primary"></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
代码:
protected void rptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var orderItem = (DataRowView)e.Item.DataItem;
Literal litCustomerName = (Literal)e.Item.FindControl("litCustomerName");
Literal litName = (Literal)e.Item.FindControl("litName");
Literal litEventType= (Literal)e.Item.FindControl("litEventType");
Literal litEventDate = (Literal)e.Item.FindControl("litEventDate");
Label lblStatus = (Label)e.Item.FindControl("lblStatus");
// set the row data e.g.
litCustomerName.Text = orderItem["CustomerName"].ToString();
switch(orderItem["EventStatus"].ToString())
{
case "accepted":
lblStatus.CssClass = "label label-accepted";
break;
case "rejected":
lblStatus.CssClass = "label label-rejected";
break;
// etc...
}
}
}
注意:我对pendingOrders
正在使用的类型做了一些假设,为了演示,我假设该类型被称为Order
,你&# 39;我想调整它以满足您的需求。