Why doesn't binding expression work with C# expressions?

后端 未结 1 1454
小蘑菇
小蘑菇 2021-02-11 09:47

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?



        
相关标签:
1条回答
  • 2021-02-11 10:22

    Try this:

    <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>
    

    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>
    

    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.

    0 讨论(0)
提交回复
热议问题