【问题标题】:ASP.NET inline expressions not working in CssClass attribute?ASP.NET 内联表达式在 CssClass 属性中不起作用?
【发布时间】:2013-01-30 18:00:14
【问题描述】:

我有以下代码:

<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),编译器抱怨。
  • 任何服务器标记(
  • 这是在中继器控件中。等等,代码里就有。
  • 如果我删除内联语句后的“activestep”类,内联语句仍然会出现,尽管有一次我在 HTML 中根本没有出现类属性。

有什么想法吗?感谢您的帮助!

【问题讨论】:

    标签: asp.net vb.net webforms web-controls


    【解决方案1】:

    您可以使用 String.Format() 构建 CssClass

    CssClass='<%# String.Format("{0} activestep", If(condition, "truestring", "falsestring"))%>'>
    

    【讨论】:

    • 后端不创建If函数,CssClass=''>
    • @etlds 那是 c# 但问题需要 vb.net。
    【解决方案2】:

    我不确定您是否可以在 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>
    

    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";
            }
        }  
    }
    

    【讨论】:

    • 很好的解决方案,但我希望不必添加到 Codebehind。这意味着我必须在我的链接按钮上放置一个 ID,并添加许多真正应该在一行中处理的代码。虽然我可能会走这条路,但你知道为什么原来的解决方案不起作用吗?
    • 我不确定您是否可以在 cssclass 属性上编写表达式。看看我的编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2014-06-29
    相关资源
    最近更新 更多