我有以下代码:
<asp:LinkButton runat="server"
CommandName="SwitchStep"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
CssClass="<%# some conditional code here %> activestep">
Step <%# Container.ItemIndex + 1 %>: <%# DataBinder.Eval(Container.DataItem, "ID")%>
</asp:LinkButton>
内联语句适用于CommandArgument
属性,我知道它们在text属性中有效。但是,出于某种原因,在CssClass
属性中,内联语句在HTML输出中结束(未解析)!怎么了?
在Chrome中:
<a class="<%= 'steptab' %>" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("StepControl:_ctl1:_ctl0", "", true, "", "", false, true))'>
Step 1: section_name</a>
有没有人遇到过这个?我不确定为什么这不起作用。这似乎不符合逻辑,我有点沮丧。
一些注意事项:
有什么想法吗?谢谢你的帮助!
答案 0 :(得分:1)
您可以使用String.Format()
CssClass='<%# String.Format("{0} activestep", If(condition, "truestring", "falsestring"))%>'>
答案 1 :(得分:0)
我不确定您是否可以在CssClass
属性中编写表达式。试试这个:
<asp:LinkButton runat="server"
CommandName="SwitchStep"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
CssClass="<%$ Iif(condition, "activestep", "") %>">
Step <%# Container.ItemIndex + 1 %>: <%# DataBinder.Eval(Container.DataItem, "ID")%>
</asp:LinkButton>
另一方面,您可以使用ItemDataBound
事件处理程序来包装您的条件,并在Repeater中的控件上设置CssClass
属性。看看
在WEbForm中,添加对ItemDataBound的引用
<asp:Repeater id="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server">
...
</asp:Repeater>
' This event is raised for the header, the footer, separators, and items.
Public Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
' Execute the following logic for Items and Alternating Items.
if e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim p as Product = CType(e.Item.DataItem, Product) ' cast to your entity just a sample
If p.Active Then ' check some condition
CType(e.Item.FindControl("Your_LinkButtonId"), LinkButton).CssClass = "activestep"
End If
End if
End Sub
// This event is raised for the header, the footer, separators, and items.
public void Repeater1_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Product p = (Product)e.Item.DataItem; // cast to your entity just a sample
if (p.Active) // check some condition
{
((LinkButton)e.Item.FindControl("Your_LinkButtonId")).CssClass = "activestep";
}
}
}