Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何处理数据绑定控件中的空值?_Asp.net_Sql Server_Vb.net_Label_Databound - Fatal编程技术网

Asp.net 如何处理数据绑定控件中的空值?

Asp.net 如何处理数据绑定控件中的空值?,asp.net,sql-server,vb.net,label,databound,Asp.net,Sql Server,Vb.net,Label,Databound,我试图限制ASP.NET标签控件中显示的字符,这些字符的文本值数据绑定到SQL数据库。如果数据库中的字段不是空的,我就没有任何问题。如果它们为NULL,则会出现异常“对象引用未设置为对象的实例” 我的代码如下: <asp:Label ID="LabelDescriptionLabel" runat="server" Text='<%# IIf(Not IsDBNull(Item.LabelDescription), IIf(Item.LabelDescription.ToString

我试图限制ASP.NET标签控件中显示的字符,这些字符的文本值数据绑定到SQL数据库。如果数据库中的字段不是空的,我就没有任何问题。如果它们为NULL,则会出现异常“对象引用未设置为对象的实例”

我的代码如下:

<asp:Label ID="LabelDescriptionLabel" runat="server" Text='<%# IIf(Not IsDBNull(Item.LabelDescription), IIf(Item.LabelDescription.ToString.Length > 30, Left(Item.LabelDescription, 30) & "...", Item.LabelDescription), Item.LabelDescription)%>' />

我还尝试了以下方法,但没有成功:

<asp:Label ID="LabelDescriptionLabel" runat="server" Text='<%# IIf(Not IsNothing(Item.LabelDescription), IIf(Item.LabelDescription.ToString.Length > 30, Left(Item.LabelDescription, 30) & "...", Item.LabelDescription), Item.LabelDescription)%>' />

我正在使用ASP.NET和VB.NET。我也尝试过使用CSS的“文本溢出:省略号”方法,但是我有一些表格式问题没有解决

有没有办法处理这些空值并防止运行时异常


谢谢

您遇到的一个问题是,旧的
IIF
是一个函数,它在决定返回什么之前对所有部分进行求值

IIF(Not IsDBNull(Item.LabelDescription), TruePart, FalsePart)
表达式是否为真并不重要,
TruePart

FalsePart
语句将始终执行。因为它们包含对可能是DBNull/Nothing/null的内容的引用,所以会出现一个错误

还有一点是Nothing与DBNull不同,如果
Item
是一个可以为Nothing/null的对象,那么测试Nothing/null的描述将是有问题的

自2008年左右开始提供。结构相同,但它仅评估测试条件和适用于以下情况的真假表达式:

If( Condition, TruePart, FalsePart )
“短路”会阻止对不适用的表达式/部件进行求值。只要代码的其余部分有效,您就应该能够从
IIF
函数中剪辑一个
I
s

由于您希望对象为Nothing,因此我将使用
Nothing
关键字,而不是VB
IsNothing
函数:

If(Item IsNot Nothing), 
        If(Item.LabelDescription.ToString.Length > 30, x, y), y)
If运算符与If/Then/Else语句块不同。

使用新的(ish)而不是
IIF
来缩短计算
IIF
将对所有元素/子句进行求值,以便
Item.LabelDescription.ToString.Length
将导致异常,即使您尝试对其进行测试。