If TextBox1.Text = "" Then
            Response.Write("<script>window.alert('用户名不能为空!')</script>")
        ElseIf TextBox2.Text = "" Then
            Response.Write("<script>window.alert('密码不能为空!')</script>")
        Else
            Try
                Dim ssconstr As String = "server=192.168.0.1;database=cszjc-website;user id=maverickps;password=zsjyc;"
                Dim ssqlstr As String = "select * from login where username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "'"
                Dim ssobjconn As New SqlConnection(ssconstr)
                Dim ss As New SqlCommand(ssqlstr, ssobjconn)
                ssobjconn.Open()
                Dim mydr As SqlDataReader = ss.ExecuteReader()                If mydr.Read Then
                    Response.Redirect("Sd-login/login.aspx")
                Else
                    Response.Redirect("error.htm")
                End If
            Catch ex As Exception
                Response.Write("<script>window.alert('登陆失败!')</script>")
            End Try
        End If/////////////////////////////////////////////
Dim xy As String
        xy = DropDownList1.SelectedValue.ToString()
        Dim user, pass As String
        user = TextBox3.Text
        pass = TextBox4.Text        If user = "" Then
            Response.Write("<script>window.alert('用户名不能为空!')</script>")
        ElseIf pass = "" Then
            Response.Write("<script>window.alert('密码不能为空!')<script>")
        Else
            Try
                Dim constr As String = "server=192.168.0.1;database=netconfer;user id=maverickps;password=zsjyc;"
                Dim sqlstr As String = "select * from xyuser where name='" + user + "' and pass='" + pass + "' and status='" + xy + "'"
                Dim objconn As New SqlConnection(constr)
                Dim ss As New SqlCommand(sqlstr, objconn)
                objconn.Open()
                Dim mydr As SqlDataReader = ss.ExecuteReader()                If mydr.Read Then
                    Response.Redirect("careersystem/departsystem/depatsys.aspx?xy=" & DropDownList1.SelectedValue.ToString())
                Else
                    Response.Write("error-1.htm")
                End If
            Catch ex As Exception
                Response.Write("<script>window.alert('登陆失败!')</script>")
            End Try
        End If第一段代码中对密码可以进行控制判断,textbox2.text = ""
但第二段代码中对密码就不能进行空值判断了textbox4.txt = ""
还有,我想对DropDownList1.SelectedValue.ToString()的值进行判断,要求用户必须进行选择
下面是dropdownlist1的代码
<asp:dropdownlist id="dropdownlist1" runat="server">
<asp:listitem value="" selected="true"></asp:listitem>
<asp:listitem value="1">北京</asp:listitem>
<asp:listitem value="2">上海</asp:listitem>
</asp:dropdownlist>就是对value值进行判断,谢谢各位。

解决方案 »

  1.   

    If TextBox1.Text = "" Then
                Response.Write("<script>window.alert('用户名不能为空!')</script>")
                return
            ElseIf TextBox2.Text = "" Then
                Response.Write("<script>window.alert('密码不能为空!')</script>")
                return
            Else
      

  2.   

    DropDownList1.Items.Insert(0,"请选择城市")
    if DropDownList1.SelectedText.ToString()="请选择城市" then
    Response.Write("<script>window.alert('请选择城市!')</script>")
    return
      

  3.   

    你最好给DropDownList添加Item的时候就在最前面加上一个“请选择城市”,把他的Selected属性设为true,判断的时候直接判断DropDownList1.SelectedText.ToString()是否等于“请选择城市”就可以了~
    DropDownList1.Items.Insert(0,"请选择城市")   //为下拉框添加第一项~
      

  4.   

    谢谢两位,不过我用了这段代码,还是不行
    If TextBox3.Text = "" Then
                Response.Write("<script>window.alert('用户名不能为空!')</script>")
                return
            ElseIf TextBox4.Text = "" Then
                Response.Write("<script>window.alert('密码不能为空!')</script>")
                return
            Else
      

  5.   

    你为什么不用RequiredFieldValidator ?
      

  6.   

    以下是代码,用户名可以进行控制判断,但密码和下拉菜单都不行。
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim user, pass As String
            user = TextBox3.Text
            pass = TextBox4.Text        Dim xy As String
            xy = DropDownList1.SelectedValue.ToString()        If user= "" Then
                Response.Write("<script>window.alert('用户名不能为空!')</script>")
                Return
            ElseIf pass = "" Then
                Response.Write("<script>window.alert('密码不能为空!')<script>")
                Return
            ElseIf DropDownList1.SelectedValue.ToString() = "请选择" Then
                Response.Write("<script>window.alert('不能为空!')<script>")
                Return
            Else
                Try
                    Dim constr As String = "server=192.168.0.1;database=netconfer;user id=maverickps;password=zsjyc;"
                    Dim sqlstr As String = "select * from xyuser where name='" + user + "' and pass='" + pass + "' and status='" + xy + "'"
                    Dim objconn As New SqlConnection(constr)
                    Dim ss As New SqlCommand(sqlstr, objconn)
                    objconn.Open()
                    Dim mydr As SqlDataReader = ss.ExecuteReader()                If mydr.Read Then
                        Response.Redirect("careersystem/departsystem/depatsys.aspx?xy=" & DropDownList1.SelectedValue.ToString())
                    Else
                        Response.Write("error-1.htm")
                    End If
                Catch ex As Exception
                    Response.Write("<script>window.alert('登陆失败!')</script>")
                End Try
            End If
        End Sub