如何通过更新按钮进行回发而不更新新的文本框值

时间:2015-12-09 20:58:25

标签: asp.net vb.net postback pageload

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

这是我的页面加载:

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

1 个答案:

答案 0 :(得分:1)

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

  

“我有文本框的值是在页面加载时设置的”

但是,我可以理解你需要这样做:

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

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

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