如何克服执行 post 返回而不更新新文本框值的更新按钮

How to over come an update button doing a post back and not updating new text box values

我有一些文本框,其值是在页面加载时设置的,每次更改下拉菜单时也会设置它们。如果有任何更改,我有一个更新按钮来更新文本框。如果您更改文本框并点击更新,则新值不会更新。我相信这是因为单击按钮会触发回发。我不能只在页面加载时绑定,因为我需要在下拉列表更改时更改文本框。任何使我的更新实际更新文本框中的值的建议解决方法??

这是我的页面加载:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    If Session.Contents.Count = 0 Or Session("LoggedInSecurityLevel") Is Nothing Then
        Session("ReturnCode") = 1
        Response.Redirect("logon.aspx?ReturnUrl=%2f" & Request.RawUrl)
    Else

        If IsPostBack = False Then
            If Session("LoggedInSecurityLevel") = "99" Then
                dd_pharm.Visible = True
                GetPharmInfo(Session("LoggedInSecurityLevel").ToString())
            End If
        Else
            GetPharmInfo(Session("LoggedInSecurityLevel").ToString())
        End If
    End If

End Sub

这里是按钮点击事件:

Protected Sub btn_update_Click(sender As Object, e As EventArgs) Handles btn_update.Click

        Dim Conn As New System.Data.SqlClient.SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("PharmacyConnectionString").ToString)
    Conn.Open()

    'Dim ds As New System.Data.DataSet
    Dim dt As New DataTable()
    Dim DataAdapter As New System.Data.SqlClient.SqlDataAdapter
    Dim CommandType As System.Data.CommandType
    Dim CommandText As String
    Dim Connection As System.Data.SqlClient.SqlConnection

    'Call connection and sp............................................................................
    CommandType = Data.CommandType.StoredProcedure
    CommandText = "spu_Pharmacies"
    Connection = Conn


    Dim DataCommand As New System.Data.SqlClient.SqlCommand(CommandText, Connection)
    DataCommand.CommandType = CommandType

    DataCommand.Parameters.AddWithValue("@PharmacyName", tb_pharmname.Text)
    DataCommand.Parameters.AddWithValue("@PharmacyContact", tb_contact.Text)
    DataCommand.Parameters.AddWithValue("@PharmacistName", tb_pharmacist.Text)
    DataCommand.Parameters.AddWithValue("@PharmacyPhone", tb_phone.Text)
    DataCommand.Parameters.AddWithValue("@Fax", tb_fax1.Text)
    DataCommand.Parameters.AddWithValue("@Fax2", tb_fax2.Text)
    DataCommand.Parameters.AddWithValue("@Email", tb_email.Text.ToString)
    DataCommand.Parameters.AddWithValue("@Text", tb_text.Text)
    DataCommand.Parameters.AddWithValue("@Notes", tb_notes.Text)

    DataCommand.Parameters.AddWithValue("@SendToFax", rb_fax1.SelectedValue)
    DataCommand.Parameters.AddWithValue("@SendToFax2", rb_fax2.SelectedValue)
    DataCommand.Parameters.AddWithValue("@SendToEmail", rb_email.SelectedValue)
    DataCommand.Parameters.AddWithValue("@SendToText", rb_text.SelectedValue)

    DataCommand.Parameters.AddWithValue("@SecurityCode", lbl_securitycode.Text)

    DataAdapter.SelectCommand = DataCommand

    DataAdapter.Fill(dt)

    Conn.Close()

    Response.Redirect(Request.RawUrl)

End Sub

这里是设置文本框的地方:

  Private Function GetPharmInfo(ByVal SecurityLevel As String) As Integer

        Dim Conn As New System.Data.SqlClient.SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("PharmacyConnectionString").ToString)

        Conn.Open()

        'Dim ds As New System.Data.DataSet
        Dim dt As New DataTable()
        Dim DataAdapter As New System.Data.SqlClient.SqlDataAdapter
        Dim CommandType As System.Data.CommandType
        Dim CommandText As String
        Dim Connection As System.Data.SqlClient.SqlConnection

        'Call connection and sp............................................................................
        CommandType = Data.CommandType.StoredProcedure
        CommandText = "sps_PharmacyInfo"
        Connection = Conn

        Dim DataCommand As New System.Data.SqlClient.SqlCommand(CommandText, Connection)
        DataCommand.CommandType = CommandType

        If dd_pharm.Visible = True Then
            If dd_pharm.SelectedIndex.ToString() = "-1" Then
                DataCommand.Parameters.AddWithValue("@SecurityCode", "1")
            Else
                DataCommand.Parameters.AddWithValue("@SecurityCode", dd_pharm.SelectedValue.ToString())
            End If
        Else
            DataCommand.Parameters.AddWithValue("@SecurityCode", SecurityLevel)
        End If

        DataAdapter.SelectCommand = DataCommand

        DataAdapter.Fill(dt)

        If dt.Rows(0)("Active").ToString() = 1 Then
            tb_active.Text = "Yes"
        Else
            tb_active.Text = "No"
        End If

        tb_pharmname.Text = dt.Rows(0)("PharmacyName").ToString()
        tb_contact.Text = dt.Rows(0)("PharmacyContact").ToString()
        tb_pharmacist.Text = dt.Rows(0)("PharmacistName").ToString()
        tb_phone.Text = dt.Rows(0)("PharmacyPhone").ToString()
        tb_fax1.Text = dt.Rows(0)("Fax").ToString()
        tb_fax2.Text = dt.Rows(0)("Fax2").ToString()
        tb_email.Text = dt.Rows(0)("Email").ToString()
        tb_text.Text = dt.Rows(0)("Text").ToString()
        tb_notes.Text = dt.Rows(0)("Notes").ToString()

        'RADIO BUTTONS
        If dt.Rows(0)("SendToFax").ToString() = 0 Then
            rb_fax1.SelectedValue = dt.Rows(0)("SendToFax").ToString()
            lbl_fax1.BackColor = System.Drawing.Color.LightCoral
        Else
            rb_fax1.SelectedValue = dt.Rows(0)("SendToFax").ToString()
            lbl_fax1.BackColor = System.Drawing.Color.LightGreen
        End If

        If dt.Rows(0)("SendToFax2").ToString() = 0 Then
            rb_fax2.SelectedValue = dt.Rows(0)("SendToFax2").ToString()
            lbl_fax2.BackColor = System.Drawing.Color.LightCoral
        Else
            rb_fax2.SelectedValue = dt.Rows(0)("SendToFax2").ToString()
            lbl_fax2.BackColor = System.Drawing.Color.LightGreen
        End If

        If dt.Rows(0)("SendToEmail").ToString() = 0 Then
            rb_email.SelectedValue = dt.Rows(0)("SendToEmail").ToString()
            lbl_email.BackColor = System.Drawing.Color.LightCoral
        Else
            rb_email.SelectedValue = dt.Rows(0)("SendToEmail").ToString()
            lbl_email.BackColor = System.Drawing.Color.LightGreen
        End If

        If dt.Rows(0)("SendToText").ToString() = 0 Then
            rb_text.SelectedValue = dt.Rows(0)("SendToText").ToString()
            lbl_text.BackColor = System.Drawing.Color.LightCoral
        Else
            rb_text.SelectedValue = dt.Rows(0)("SendToText").ToString()
            lbl_text.BackColor = System.Drawing.Color.LightGreen
        End If

        lbl_updated.Text = "Last Updated: " & dt.Rows(0)("UpdatedDate").ToString()
        lbl_securitycode.Text = dt.Rows(0)("SecurityCode").ToString()


        Conn.Close()

    End Function

我没有看到设置初始文本框值的页面加载代码,如您在此处所述:

"I have text boxes who's values are set on page load"

但是,我猜你需要这样做:

在页面加载时处理文本框值的事件处理程序或子程序中添加 IsPostBack = False 条件:

    If IsPostBack = False Then
        'assign your textbox values 
        tb_pharmname.Text = "something"
        tb_contact.Text = "something else"
        'etc 
    End If

如果上面的分配没有那个条件,它们将在 btn_update_Click 有机会执行之前在回发时重置回原始值。