1

我有以下代码:

<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属性中起作用,我知道它们在文本属性中起作用。但是,出于某种原因,在CssClass属性中,内联语句在 HTML 输出中结束(未解析)!我勒个去?

在 Chrome 中:

<a class="&lt;%= 'steptab' %&gt;" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("StepControl:_ctl1:_ctl0", "", true, "", "", false, true))'>
            Step 1: section_name</a>

有没有人遇到过这个?我不确定为什么这不起作用。这似乎不合逻辑,我有点沮丧。

一些注意事项:

  • 如果我在这里放一个不属于的字符,例如“?” (这是VB),编译器抱怨。
  • 任何服务器标记(<%、<%# 等)都会显示在 HTML 中。
  • 这是在中继器控件内。等等,那是在代码中。
  • 如果我在内联语句之后删除“activestep”类,内联语句仍然会出现,尽管有一次我根本没有出现在 HTML 中的类属性。

有任何想法吗?谢谢你的帮助!

4

2 回答 2

1

您可以使用构建 CssClassString.Format()

CssClass='<%# String.Format("{0} activestep", If(condition, "truestring", "falsestring"))%>'>
于 2013-01-30T22:12:35.643 回答
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事件处理程序来包装您的条件并CssClass在您的中继器内的控件上设置属性。看一看

在 WEBForm 中,添加对 ItemDataBound 的引用

<asp:Repeater id="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server">
...
</asp:Repeater>

VB.NET

' 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

C#

// 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";
        }
    }  
}
于 2013-01-30T18:45:56.283 回答