本文介绍了为什么不结合使用C#的前pressions前pression工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code编译,但不起作用。据我所知,数据绑定前pressions允许他们任何有效的C#code。所以,我在做什么错在这里?

The following code compiles, but doesn't work. As far as I know data-binding expressions allow any valid C# code in them. So what am I doing wrong here?

<asp:Panel CssClass='<%# ("my-class") %>' runat="server" ID="myPannel">
   Blah
</asp:Panel>

<% this.myPannel.DataBind(); %>

我没有得到任何错误。它只是没有任何渲染类属性。我没有试过用同样倒霉的括号。

I am not getting any errors. It just doesn't render the class attribute whatsoever. I tried without the parenthesis with the same bad luck.

请注意,我需要一个前pression中的CssClass属性进行评估,我期待那前pression的结果分配给类属性。这就是为什么我强调了这通过封闭串入括号。

Please note, I need an expression to be evaluated in CssClass attribute and I am expecting the result of that expression to be assigned to the class attribute. This is why I emphasized this by enclosing the string into parenthesis.

推荐答案

试试这个:

<html>
<head runat="server">
    <title></title>
    <script runat="server">
        protected void
            Page_Load(object sender, EventArgs e)
        {
            this.myPannel.DataBind();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Panel CssClass='<%# ("my-class") %>'
            runat="server"
            ID="myPannel">
            Blah
        </asp:Panel>
    </form>
</body>
</html>

或者只是切换您的直列code片段的顺序:

Or just switch the order of your inline code snippets:

<body>
    <% this.myPannel.DataBind(); %>
    <form id="form1" runat="server">
        <asp:Panel CssClass='<%# ("my-class") %>'
            runat="server"
            ID="myPannel">
            Blah
        </asp:Panel>
    </form>
</body>

或者简单地使用:

Or simply use:

<% this.myPannel.CssClass = "my-class";%>
<form id="form1" runat="server">
    <asp:Panel
        runat="server"
        ID="myPannel">
        Blah
    </asp:Panel>
</form>

在这三种情况下,你必须确保前被在页面的生命周期处理的控制的实际直列code中的控制属性更新

In all three cases, you have to make sure that the control property is updated before the actual inline code of the control is being processed in the page's life cycle.

这篇关于为什么不结合使用C#的前pressions前pression工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 08:52