注册 登录
编程论坛 ASP.NET技术论坛

为什么取不到dropdownlist的值

kiska3088915 发布于 2010-12-01 21:10, 1272 次点击
只有本站会员才能查看附件,请 登录

又得来麻烦各位大虾了,如图,这是一个网站的注册页面,其他的没问题,就剩下选择生日这个功能了,一开始觉得很简单,没放心上,最后收尾工作准备把这个功能搞定的时候发现,这段代码竟然取不到3个dropdownlist被选中项的text值,        
        If DropDownList1.SelectedIndex <> 0 Then
            birthdayyear = DropDownList1.SelectedItem.Text
        End If
        If DropDownList2.SelectedIndex <> 0 Then
            birthdaymonth = DropDownList2.SelectedItem.Text
        End If
        If DropDownList3.SelectedIndex <> 0 Then
            birthdayday = DropDownList3.SelectedItem.Text
        End If
        birthday = birthdayyear + "/" + birthdaymonth + "/" + birthdayday
不论怎么弄,最后birthday的值都是“//”,很疑惑,求大虾解释
8 回复
#2
冰镇柠檬汁儿2010-12-01 21:47
你设断点跟一下程序,告诉我DropDownList1.SelectedIndex和DropDownList1.SelectedItem.Text的值是什么?
#3
kiska30889152010-12-01 21:56
回复 2楼 冰镇柠檬汁儿
报告头,看了一下,值都是“please choose”
#4
冰镇柠檬汁儿2010-12-01 22:35
值都知道了,还不明白问题出在什么地方了吗?
#5
kiska30889152010-12-01 22:50
回复 4楼 冰镇柠檬汁儿
额,真不明白,问题是我调试时的确选择了出生年月啊,所以照理来说selectedindex不应该为0的,但是点submit的时候就是没把选中的数据读进去啊,我也不知道是怎么回事,求指教
#6
冰镇柠檬汁儿2010-12-01 22:59
submit按钮是.NET的控件吗?还是html的控件?能把submit响应的方法的代码发上来吗,让大家帮你看看
#7
kiska30889152010-12-01 23:19
回复 6楼 冰镇柠檬汁儿
是.net控件
附上代码,麻烦诸位了

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim logname As String
        Dim logpsw As String
        Dim username As String
        Dim email As String
        Dim address As String
        Dim birthday As String = ""
        Dim birthdayyear As String = ""
        Dim birthdaymonth As String = ""
        Dim birthdayday As String = ""
        logname = TextBoxlogname.Text
        logpsw = TextBoxlogpassword.Text
        username = TextBoxusername.Text
        email = TextBoxemail.Text
        address = TextBoxaddress.Text
        Dim connect As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection
        connect.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/database/shopping system of ultra electronics website.mdb"))
        Dim cmd As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand
        cmd.Connection = connect
         = "select logname from userinfo where logname=" + "'" + logname + "'"
        Dim result As String
        

        If DropDownList1.SelectedIndex <> 0 Then
            birthdayyear = DropDownList1.SelectedItem.Text
        End If
        If DropDownList2.SelectedIndex <> 0 Then
            birthdaymonth = DropDownList2.SelectedItem.Text
        End If
        If DropDownList3.SelectedIndex <> 0 Then
            birthdayday = DropDownList3.SelectedItem.Text
        End If
        birthday = birthdayyear + "/" + birthdaymonth + "/" + birthdayday

        Try
            connect.Open()
            result = cmd.ExecuteScalar
            If result = logname Then
                Response.Write("The logname is existed")
            Else
                = "insert into userinfo (logname,[password],username,gender,address,email,birthday) values ('" + logname + "','" + logpsw + "','" + username + "','" + gender + "','" + address + "','" + email + "','" + birthday + "')"
                cmd.ExecuteNonQuery()
                Response.Redirect("Registrationsuccessful.aspx?logname=" + logname)
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
        connect.Close()

    End Sub
#8
kiska30889152010-12-02 10:18
我自己查了一下,好像是说要判定ispostback,但我不知道加哪,望大虾指教
#9
kiska30889152010-12-02 10:28
终于搞定了,在pageload里初始化赋值给dropdownlist前,先判定一下ispostback是否为false就行了
1