检查隐藏字段是否为空

时间:2010-08-13 06:01:15

标签: asp.net

我有一个

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

我有一些编程方面如何检查隐藏字段是否为空 我的意思是我想检查ListTwiHiddenField.items.cout==0或清空如何查看

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分: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

答案 2 :(得分:0)

你只需要检查:

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

也不需要检查null。如果要向aspx页面添加隐藏字段。当页面控件初始化时,它也会初始化。

相关问题