0

私のvbページの1つで、セッションからの値を使用してテキストボックスの値を設定しています。同じページで、レコードの編集操作を実行したいと考えています。ページの読み込み時にテキストボックスに変数を表示し、送信時に値を編集してテキストボックスから値を取得した後でも、セッションで設定した値を取得します。テキストボックスで変更された値を取得するのではなく、ページの読み込み時に割り当てられた値を表示します。助けてください。

ここに私のコードがあります、

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 に答える