0

我试图让用户选择他们的婚姻状况,这将更新数据库中的一个字段。当用户是第一次时这很好,但如果他们想再次更新它,它不会更新。

如果我选择了一个数据源,如果他们希望更新,除了第一次选择的内容之外,DDL 中不会为他们提供任何其他选项。

* ASPX *

<asp:DropDownList ID="DDLMarital" runat="server" TabIndex="1">
                <asp:ListItem Value = "Single" Text = "Single">Single</asp:ListItem>
                <asp:ListItem Value = "Married" Text = "Married">Married</asp:ListItem>
                <asp:ListItem Value = "Separated" Text = "Separated">Separated</asp:ListItem>
                <asp:ListItem Value = "Divorced" Text = "Divorced">Divorced</asp:ListItem>
            </asp:DropDownList>

** * * ASPX.VB * ****

'binding...

Protected Sub Page_Load(sender As Object, e As System.EventArgs) 处理 Me.Load

    Dim dvSql4 As DataView =
DirectCast(SqlMarital.Select(DataSourceSelectArguments.Empty), DataView)
    For Each drvSql4 As DataRowView In dvSql4
        DDLMarital.Text = drvSql4("Marital_Status").ToString()
    Next

结束子

'单击“下一步”按钮时更新数据库 Protected Sub BtnNext_Click(sender As Object, e As System.EventArgs) Handles BtnNext.Click

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand

    con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\scrummingit2.mdf;Integrated Security=True;User Instance=True"
    con.Open()

    cmd.Connection = con
    cmd.CommandText = ("UPDATE Student_Personal_Details SET Marital_Status = @Marital WHERE Student_No = @StudentNo")

    Dim maritalParam As New SqlParameter("@Marital", DDLMarital.Text)
    Dim studentNoParam As New SqlParameter("@StudentNo", lblEnrolmentStudentNumber.Text)

    lblEnrolmentStudentNumber.Text = Session("Student")

    cmd.Parameters.Add(maritalParam)
    cmd.Parameters.Add(studentNoParam)

    cmd.ExecuteNonQuery()

    con.Close()

结束子

任何人都可以帮忙吗?在谷歌上找不到任何答案几天后,现在变得绝望。

4

1 回答 1

1

您应该检查 PostBack,并且仅当它不是 PostBack 时才分配选定的值。

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
   If Not IsPostBack
        Dim dvSql4 As DataView =
    DirectCast(SqlMarital.Select(DataSourceSelectArguments.Empty), DataView)
        For Each drvSql4 As DataRowView In dvSql4
            DDLMarital.Text = drvSql4("Marital_Status").ToString()
        Next
   End If
End Sub

用户的选择目前被忽略,因为每次都会重置该值Load

于 2013-04-08T10:14:02.970 回答