尝试这个:
<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>
或者只是切换内联代码片段的顺序:
<body>
<% this.myPannel.DataBind(); %>
<form id="form1" runat="server">
<asp:Panel CssClass='<%# ("my-class") %>'
runat="server"
ID="myPannel">
Blah
</asp:Panel>
</form>
</body>
或者干脆使用:
<% this.myPannel.CssClass = "my-class";%>
<form id="form1" runat="server">
<asp:Panel
runat="server"
ID="myPannel">
Blah
</asp:Panel>
</form>
在所有这三种情况下,您必须确保在页面生命周期中处理控件的实际内联代码之前更新控件属性。