评估文本框的内容是否无效

时间:2013-04-15 00:30:37

标签: asp.net vb.net visual-studio-2010

我正在尝试评估ASP标签的内容,看它是否为null / nothing,然后用错误消息填充另一个标签。

以下是评估(编辑:已更新以包含工作代码):

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If String.IsNullOrEmpty(lb_showcoordinates.Text) Then
        nocoordinatesmessage.Text = "Please validate your meeting location above using the <b>Validate Address</b> button"
        Console.WriteLine("okay")
    Return
    End If
    Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
    conn.Open()
    Dim cmd As New OleDbCommand("INSERT INTO Events (EventTitle, EventDescription, EventDate,EventCategory, CreatedBy, StreetAddress, Town, Country, Coordinates) VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8,@f9)", conn)
    cmd.Parameters.AddWithValue("@f1", tb_eventtitle.Text)
    cmd.Parameters.AddWithValue("@f2", tb_eventdescription.Text)
    cmd.Parameters.AddWithValue("@f3", DateTime.ParseExact(tb_eventdate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture))
    cmd.Parameters.AddWithValue("@f4", dd_eventcategory.SelectedIndex + 1)
    cmd.Parameters.AddWithValue("@f5", User.Identity.Name)
    cmd.Parameters.AddWithValue("@f6", txtStreetAddress.Text)
    cmd.Parameters.AddWithValue("@f7", txtSuburb.Text)
    cmd.Parameters.AddWithValue("@f8", txtCountry.Text)
    cmd.Parameters.AddWithValue("@f9", lb_showcoordinates.Text)
    cmd.ExecuteNonQuery()
    conn.Close()
    Response.Redirect("calendar.aspx")
End Sub

不幸的是,我没有得到预期的结果;我的标签内容已经空了(故意),所以我想知道我做错了什么? VB.NET中用于评估文本框/标签/任何其他基于文本的asp控件(即.Text)的内容是否为空的正确表达式是什么?

这是我的调试过程截图:

nothing or maybe not?

正如您所见,lb_showcoordinates.Text的内容为空(或至少为“”)

2 个答案:

答案 0 :(得分:2)

您应该使用String.IsNullOrEmtpy

If String.IsNullOrEmpty(lb_showcoordinates.Text) Then
    lb_nocoordinatesmessage.Text = "Please validate your meeting location above using the <b>Validate Address</b> button"
    Console.WriteLine("okay")
End If

textbox为空时,其内容为空字符串,因此与Nothing相比无效。

答案 1 :(得分:0)

您还可以添加一个requiredfieldvalidator,要求用户输入该字段,然后您知道在调用按钮点击时它已填写。