Search code examples
asp.netvb.netpostbackpageload

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


I have text boxes who's values are set on page load, they are also set each time a dropdown is changed. I have an update button to update the text boxes if there are any changes. If you change a text box and hit update, the new values do not get updated. I believe this is because clicking the button fires the postback. I can't only bind on page load because I need to change the text boxes when the drop down changes. Any suggested workarounds to make my update actually update the values that are in the text box??

Here is my page load:

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

Here is the button click event:

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

Here is where the text boxes are set:

  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

Solution

  • I don't see the page load code that sets the initial textbox values, as you describe here:

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

    but, I can fathom a guess that you need to do this:

    In the event handler or sub that handles the textbox values at page load add IsPostBack = False consition:

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

    If the assignments above doesn't have that condition, they will be reset back to the original values on postback before btn_update_Click has a chance to execute.