Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textbox.text still retaining old value after it has been changed

Tags:

asp.net

vb.net

I have an asp.net web page with a form that contains a series of text boxes and check boxes that are populated with data from a data base when the page loads. I intend to make it so that the user can freely change the text or check values of the boxes, and then press a button. Upon pressing the button the info will be pulled from the boxes they edited and then the db will be updated according to the changes the changes made. The problem is, textbox.text and checkbox.checked still contain the values that were initially loaded into them int he page load sub, even if the text of checkbox val has been changed.

Here is my code thus far:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim lq2 As New lqDFDataContext
        Dim var = lq2.admin_GetFeatured().ToList()
        Dim i As Integer = 1

        For Each f In var
            If i = 1 Then
                isChanged.featuredID1 = f.featuredID
                title1.Text = f.title
                text1.Text = f.text
                imageURL1.Text = f.imageURL
                login1.Checked = f.loginPage
                index1.Checked = f.indexPage
                mobile1.Checked = f.mobilePage
                priority1.Text = Str(f.priority)
            End If

            If i = 2 Then
                isChanged.featuredID2 = f.featuredID
                title2.Text = f.title
                text2.Text = f.text
                imageURL2.Text = f.imageURL
                login2.Checked = f.loginPage
                index2.Checked = f.indexPage
                mobile2.Checked = f.mobilePage
                priority2.Text = Str(f.priority)
            End If

            If i = 3 Then
                isChanged.featuredID3 = f.featuredID
                title3.Text = f.title
                text3.Text = f.text
                imageURL3.Text = f.imageURL
                login3.Checked = f.loginPage
                index3.Checked = f.indexPage
                mobile3.Checked = f.mobilePage
                priority3.Text = Str(f.priority)
            End If

            If i = 4 Then
                isChanged.featuredID4 = f.featuredID
                title4.Text = f.title
                text4.Text = f.text
                imageURL4.Text = f.imageURL
                login4.Checked = f.loginPage
                index4.Checked = f.indexPage
                mobile4.Checked = f.mobilePage
                priority4.Text = Str(f.priority)
            End If

            If i = 5 Then
                isChanged.featuredID5 = f.featuredID
                title5.Text = f.title
                text5.Text = f.text
                imageURL5.Text = f.imageURL
                login5.Checked = f.loginPage
                index5.Checked = f.indexPage
                mobile5.Checked = f.mobilePage
                priority5.Text = Str(f.priority)
            End If

            If i = 6 Then
                isChanged.featuredID6 = f.featuredID
                title6.Text = f.title
                text6.Text = f.text
                imageURL6.Text = f.imageURL
                login6.Checked = f.loginPage
                index6.Checked = f.indexPage
                mobile6.Checked = f.mobilePage
                priority6.Text = Str(f.priority)
            End If

            i = i + 1


        Next
    End Sub

    Protected Sub featuredSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles featuredSubmit.Click
        Dim lq As New lqDFDataContext



        If isChanged1.Checked Then
            lq.admin_RemoveFeatured(isChanged.featuredID1)
            lq.admin_AddFeatured(title1.Text, text1.Text, imageURL1.Text, login1.Checked, index1.Checked, mobile1.Checked, Integer.Parse(priority1.Text))

        End If

        If isChanged2.Checked Then
            lq.admin_RemoveFeatured(isChanged.featuredID2)
            lq.admin_AddFeatured(title2.Text, text2.Text, imageURL2.Text, login2.Checked, index2.Checked, mobile2.Checked, Integer.Parse(priority2.Text))

        End If

        If isChanged3.Checked Then
            lq.admin_RemoveFeatured(isChanged.featuredID3)
            lq.admin_AddFeatured(title3.Text, text3.Text, imageURL3.Text, login3.Checked, index3.Checked, mobile3.Checked, Integer.Parse(priority3.Text))

        End If

        If isChanged4.Checked Then
            lq.admin_RemoveFeatured(isChanged.featuredID4)
            lq.admin_AddFeatured(title4.Text, text4.Text, imageURL4.Text, login4.Checked, index4.Checked, mobile4.Checked, Integer.Parse(priority4.Text))

        End If

        If isChanged5.Checked Then
            lq.admin_RemoveFeatured(isChanged.featuredID5)
            lq.admin_AddFeatured(title5.Text, text5.Text, imageURL5.Text, login5.Checked, index5.Checked, mobile5.Checked, Integer.Parse(priority5.Text))

        End If

        If isChanged6.Checked Then
            lq.admin_RemoveFeatured(isChanged.featuredID6)
            lq.admin_AddFeatured(title6.Text, text6.Text, imageURL6.Text, login6.Checked, index6.Checked, mobile6.Checked, Integer.Parse(priority6.Text))

        End If

    End Sub


End Class

Public Module isChanged
    Public featuredID1 As Integer
    Public featuredID2 As Integer
    Public featuredID3 As Integer
    Public featuredID4 As Integer
    Public featuredID5 As Integer
    Public featuredID6 As Integer
End Module
like image 340
xxyyxx Avatar asked Sep 07 '12 19:09

xxyyxx


2 Answers

Your load statement sets the values of the form on every page postback, it's probably overriding the original value; you need to wrap the foreach code in an If (Not Page.IsPostBack) Then .. End If statement.

HTH

like image 193
Brian Mains Avatar answered Oct 13 '22 21:10

Brian Mains


Put the pageload code into a if (!Page.IsPostback)

like image 31
Andrew Walters Avatar answered Oct 13 '22 21:10

Andrew Walters