asp.net 内联代码 <%# MyboolVal %>
我一定在这里错过了一些愚蠢的东西,但我看不到它。我的工作在他们的网站上使用内联代码,例如:
<panel runat="server" id="myid" visible='<%# MyboolVal %>'>
some stuff
</panel>
这似乎对他们来说非常有用,当满足他们的条件时,面板将显示。
我正在尝试在家里的网站上使用类似的方法(这是周五晚上,所以此时询问我的老板不是最好的主意)。我根本无法让它输出任何东西。我已经在可见字段中尝试过它,但不起作用,所以我想我应该让它在屏幕上写一些东西:
<p>some text <%# String.Format("meeee {0}", Mybool) %></p>
但我没有从内联代码中得到任何输出。出现“一些文本”,但没有“meeee”或布尔值。
我现在正在用户控件内执行此操作,但不认为这是原因。
有什么想法吗?
谢谢
编辑....
好的,感谢 Freddy Rios 的回复,我可以让文本出现,但是当我尝试这样做时:
Visible='<%= mybool %>'
我得到以下编译错误:
无法从其字符串表示形式创建 System.boolean 类型的对象可见的属性。
我对到底发生了什么感到困惑。一定有部分过程是我不明白的。
编辑 2:
我在第 123 行收到错误:
<fieldset class="myclass" id="projectarea" runat="server" visible='<%= ShowProjectSearchArea %>'>
ShowProjectSearchArea 是我的 bool 值,设置为 false。
如果我双击“错误列表”窗口中的错误,我会在弹出窗口中看到以下内容,这是我以前从未见过的:
Cannot open file '%1'. It might not be in the solution.
I must be missing something stupid here but I can not see it. My work uses inline code on their sites, for example:
<panel runat="server" id="myid" visible='<%# MyboolVal %>'>
some stuff
</panel>
That seems to work great for them, the panel will display when their condition is meet.
I am trying to use a similar approach on a site of mine at home (its late friday evening so asking my boss is not the best idea at this point). I can not get it to output anything at all. I have tried it in the visible field which didn't work, so I thought I would just get it to write something to the screen:
<p>some text <%# String.Format("meeee {0}", Mybool) %></p>
But I do not get any output from the inline code. the "some text" appears but no "meeee" or the bool value.
I am doing this inside a user control, at this moment but do not imagine that would be the cause.
any ideas please?
Thanks
EDIT....
OK so thanks to Freddy Rios for the reply I can get the text to appear but when I try that in:
Visible='<%= mybool %>'
I get the compilation error of:
Cannot create an object of type System.boolean from its string representation for the visible property.
I am confused as to what exactly is happening. There must be part of the process under the bonnet I don't get.
EDIT 2:
I get the error on line 123:
<fieldset class="myclass" id="projectarea" runat="server" visible='<%= ShowProjectSearchArea %>'>
ShowProjectSearchArea is my bool value, set to false.
If I double click the error in the Error List window I get the following in a popup, which I have never seen before:
Cannot open file '%1'. It might not be in the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
<%#
是数据绑定标记,用于为服务器端控件(尤其是数据绑定控件)设置值。<%=
是Response.Write()
的简写,它将值写入输出。所以我们将它与静态 html 元素一起使用。<%#
is databinding tag which is used to set values to server side controls, especially databound controls.<%=
is shorthand ofResponse.Write()
, it writes the value to the output. So we use it with static html elements.尝试在您的版本中使用 = 而不是 #:
# 用于数据绑定,因此在原始代码中必须在某处调用 DataBind。
Try using = instead of # in your version:
The # is for databinding, so in the original code there must be a call to DataBind somewhere.
我认为这个问题是因为可见属性期望字符串类型的值,并且您尝试使用 bool.try 将您的值转换为字符串
干杯来设置它
I think that problem is because visible property expect value of type string and you are trying to set it with bool.try to cast your value to string
Cheers