Why doesn#39;t binding expression work with C# expressions?(为什么绑定表达式不适用于 C# 表达式?)
问题描述
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.
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.
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.
这篇关于为什么绑定表达式不适用于 C# 表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么绑定表达式不适用于 C# 表达式?
基础教程推荐
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 创建属性设置器委托 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01