2010-08-13 83 views
0

我有一个检查隐藏字段为空或不

<asp:HiddenField runat="server" ID="ListTwoHiddenField" /> 

我有一些编程方面如何检查隐藏字段是否为空或不 我的意思是我要检查ListTwiHiddenField.items.cout==0或空我怎么能勾选这个

+1

你能发布更多的标记吗?包含此字段的列表,您尝试访问它的方式。并尝试确保当你键入代码时,它是正确的 - 它的'Items.Count()',而不是'items.cout'。 – Oded 2010-08-13 06:05:38

+0

不,不,没有什么像items.count方法我只是写我自己解释你的人,我想知道那种功能 – NoviceToDotNet 2010-08-13 06:08:16

回答

3

HiddenFields没有Items集合,它们只有一个Value属性,它是一个String。因此,要检查它是否是空的,所有你需要的是:

if (string.IsNullOrEmpty(ListTwoHiddenField.Value) 
{ 
} 

或者你可以使用string.IsEmptyOrWhitespace,这将检查隐藏字段的值是否只是[空格]字符。

0

你只需要检查:

if(ListTwoHiddenField.Value != String.Empty) 
{ 
    //do code 
} 

无需检查空也。如果你正在向aspx页面添加隐藏字段。当页面控件初始化时它也会初始化。

1
dont check for NUll and Empty in one line 
    like this 
    if(HiddenObj.Value != null || HiddenObj.Value.Length > 0) 
    { 
     //hidden object is not null and have length more than zero that means have some content 
    } 
    else 
    { 
    //hidden object is null or have length empty 
    } 

    this thing is going to fail 
    try like this 
    if(HiddenObj.Value != null) 
    { 
     if(HiddenObj.Value.Length > 0) 
     { 
     //hidden object is not null and have length more than zero that means have some content 
     }else 
     { 
     // hidden object value is empty 
     } 


    }else 
    { 
    // hidden object is Null 
    } 

// from santosh kakani 
+1

或只是去与string.IsNullOrEmpty(HiddenObj.Value)? – Fishcake 2012-04-26 12:20:50

+0

甚至在你的第一个例子中用你的OR(||)替换为AND(&&),因为如果第一个条件失败,第二个不会被检查,所以没有NullReferenceException – Fishcake 2012-04-26 12:23:03