0

在我的一个 vb 页面中,我通过来自会话的值设置文本框的值。在同一页面中,我想对记录执行编辑操作。在页面加载时,我在文本框中显示了变量,并且在我提交并从文本框中获取值时编辑值后,它仍然采用我通过会话设置的值。它不采用文本框中更改的值,而是显示页面加载时分配的值。请帮忙。

这是我的代码,

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.SessionState.HttpSessionState
Imports System.Drawing
Imports System.Drawing.Printing
Partial Class Default2
    Inherits System.Web.UI.Page
    Dim cn As New SqlConnection
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cn = New SqlConnection("Data Source=nnn-PC;Initial Catalog=Faculty Housing;Persist Security Info=True;User ID=sa;Password=nnn;")
        cn.Open()
        Dim var As String
        var = Session("S2").ToString()
        TextBox1.Text = var
        Session.Remove("S2")
        cmd = New SqlCommand("select * from HouseDetails where OccupantName= '" & TextBox1.Text & "' ", cn)
        dr = cmd.ExecuteReader
        While (dr.Read)
            Label1.Text = dr(1)
            Label2.Text = dr(2)
            Label3.Text = dr(3)
            TextBox3.Text = dr(4)
            DropDownList3.SelectedItem.Text = dr(5)
            TextBox2.Text = dr(6)
            TextBox4.Text = dr(7)
            TextBox5.Text = dr(8)
            DropDownList4.Text = dr(9)
        End While
        cn.Close()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        cn = New SqlConnection("Data Source=nnn-PC;Initial Catalog=Faculty Housing;Persist Security Info=True;User ID=sa;Password=nnn;")
        cn.Open()
        Dim query As String = ("UPDATE HouseDetails SET OccupantName='" & TextBox1.Text & "' where HouseNum='" & Label3.Text & "'")
        cmd = New SqlCommand(query, cn)
        Dim x As Integer = cmd.ExecuteNonQuery()
        cn.Close()

    End Sub

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class
4

3 回答 3

0

我想你错过了检查IsPostBack这是一个新的请求还是提交。

If (Not IsPostBack) Then
  ' populate for first load
End If
于 2013-05-29T07:36:42.917 回答
0

您必须将代码放在 IsPostback 属性中。

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.SessionState.HttpSessionState
Imports System.Drawing
Imports System.Drawing.Printing
Partial Class Default2
    Inherits System.Web.UI.Page
    Dim cn As New SqlConnection
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If (!IsPostBack) Then
        cn = New SqlConnection("Data Source=nnn-PC;Initial Catalog=Faculty Housing;Persist Security Info=True;User ID=sa;Password=nnn;")
        cn.Open()
        Dim var As String
        var = Session("S2").ToString()
        TextBox1.Text = var
        Session.Remove("S2")
        cmd = New SqlCommand("select * from HouseDetails where OccupantName= '" & TextBox1.Text & "' ", cn)
        dr = cmd.ExecuteReader
        While (dr.Read)
            Label1.Text = dr(1)
            Label2.Text = dr(2)
            Label3.Text = dr(3)
            TextBox3.Text = dr(4)
            DropDownList3.SelectedItem.Text = dr(5)
            TextBox2.Text = dr(6)
            TextBox4.Text = dr(7)
            TextBox5.Text = dr(8)
            DropDownList4.Text = dr(9)
        End While
        cn.Close()
  End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        cn = New SqlConnection("Data Source=nnn-PC;Initial Catalog=Faculty Housing;Persist Security Info=True;User ID=sa;Password=nnn;")
        cn.Open()
        Dim query As String = ("UPDATE HouseDetails SET OccupantName='" & TextBox1.Text & "' where HouseNum='" & Label3.Text & "'")
        cmd = New SqlCommand(query, cn)
        Dim x As Integer = cmd.ExecuteNonQuery()
        cn.Close()

    End Sub

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class
于 2013-05-29T07:37:35.810 回答
0

使用“If (Page.IsPostBack = false) Then”行添加回发预防,以便不再将相同的值分配给文本框,

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.SessionState.HttpSessionState
Imports System.Drawing
Imports System.Drawing.Printing
Partial Class Default2
    Inherits System.Web.UI.Page
    Dim cn As New SqlConnection
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If (Page.IsPostBack = false) Then
        cn = New SqlConnection("Data Source=nnn-PC;Initial Catalog=Faculty Housing;Persist Security Info=True;User ID=sa;Password=nnn;")
        cn.Open()
        Dim var As String
        var = Session("S2").ToString()
        TextBox1.Text = var
        Session.Remove("S2")
        cmd = New SqlCommand("select * from HouseDetails where OccupantName= '" & TextBox1.Text & "' ", cn)
        dr = cmd.ExecuteReader
        While (dr.Read)
            Label1.Text = dr(1)
            Label2.Text = dr(2)
            Label3.Text = dr(3)
            TextBox3.Text = dr(4)
            DropDownList3.SelectedItem.Text = dr(5)
            TextBox2.Text = dr(6)
            TextBox4.Text = dr(7)
            TextBox5.Text = dr(8)
            DropDownList4.Text = dr(9)
        End While
        cn.Close()
  End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        cn = New SqlConnection("Data Source=nnn-PC;Initial Catalog=Faculty Housing;Persist Security Info=True;User ID=sa;Password=nnn;")
        cn.Open()
        Dim query As String = ("UPDATE HouseDetails SET OccupantName='" & TextBox1.Text & "' where HouseNum='" & Label3.Text & "'")
        cmd = New SqlCommand(query, cn)
        Dim x As Integer = cmd.ExecuteNonQuery()
        cn.Close()

    End Sub

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class
于 2013-05-29T08:06:34.340 回答