将 Eval(“bitValue”) 转换为 Bool

发布于 2024-09-14 06:29:44 字数 480 浏览 3 评论 0原文

我在 ItemTemplate 中有一个带有 HyperLink 控件的列表视图。我想如果返回值为 0(假)则显示链接,如果返回值为 1(真)则不显示链接。

到目前为止,我有这个:

<asp:HyperLink runat="server" ID="lnkReview"
  NavigateUrl='<%# Eval("EnquiryID", @"selectcompany.aspx?enq={0}")%>'
  Text="Review Enquiry"
  Visible='<%# ((bool)Eval("Locked"))==true? false : true %>' />

...但这会呈现“指定的转换无效”异常。

我在其他地方看到的例子表明这应该可行。我可以确认 Locked 列只返回 0 或 1(来自 SQL Server)——当然这些应该很容易从 bit/int 转换为 bool?

I have a list view with a HyperLink control within the ItemTemplate. I want to display the link if a returned value is 0 (false), and not display the link if it is 1 (true).

So far I have this:

<asp:HyperLink runat="server" ID="lnkReview"
  NavigateUrl='<%# Eval("EnquiryID", @"selectcompany.aspx?enq={0}")%>'
  Text="Review Enquiry"
  Visible='<%# ((bool)Eval("Locked"))==true? false : true %>' />

...but this renders an 'Specified cast is not valid' exception.

Examples I've seen elsewhere suugest this should work. I can confirm that the Locked column only returns 0 or 1 (from SQL Server) - surely these should be easily cast from bit/int to bool??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2024-09-21 06:29:44

如果 Locked 是一个 int 你应该这样做:

<%# ((int)Eval("Locked")) == 1 ? true : false %>

但是这也应该有效,所以当 Locked > > 时它返回 true 0

<%# !((int)Eval("Locked") == 0) %>

无论提到 Locked 包含 0 或 1,它仍然是一个 INT,由于某种原因可能包含值 > 0。 1. 因此,我发现检查 == 0 而不是 == 1 是一个很好的做法。我们不知道 Locked 的用途是什么,将来设计可能会改变,以便 Locked 可以包含一个值 > 1.

If Locked is an int you should do this:

<%# ((int)Eval("Locked")) == 1 ? true : false %>

But then this should work too, so it returns true when Locked > 0

<%# !((int)Eval("Locked") == 0) %>

No matter it is mentioned that Locked contains 0 or 1. It is still an INT which can for some reason contain values > 1. Therefore i find it good practice to do the check on == 0 instead of == 1. We don't know what Locked is used for and in the future the design could change so that Locked can contain a value > 1.

妄断弥空 2024-09-21 06:29:44

SQL 是一个奇怪的世界,其中位可以具有三种状态:0、1 和 null!
所以这意味着 Eval("Locked") 是一个可以为 null 的 bool 类型,而不是一个普通的 bool
如果位值为 null,则转换为 bool 将无效,您必须首先检查这一点:

(Eval("Locked")!=null && (bool)Eval("Locked")==true)

这假设 null 应映射为 false。

SQL is a strange world where bits can have three states 0, 1 and null!
So this means that Eval("Locked") is a nullable type bool, not a plain bool
A cast to bool will not be valid if the bit value is null, you have to check that first:

(Eval("Locked")!=null && (bool)Eval("Locked")==true)

This assumes that null should be mapped to false.

厌味 2024-09-21 06:29:44
Checked='<%# Eval("SEND_EMAIL") == DBNull.Value ? false : Convert.ToBoolean(Eval("SEND_EMAIL")) %>'  
Checked='<%# Eval("SEND_EMAIL") == DBNull.Value ? false : Convert.ToBoolean(Eval("SEND_EMAIL")) %>'  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文