textbox.text在更改后仍保留旧值

时间:2012-09-07 19:32:50

标签: asp.net vb.net

我有一个asp.net网页,其中包含一系列文本框和复选框,这些文本框和复选框在页面加载时填充了数据库中的数据。我打算这样做,以便用户可以自由更改文本或检查框的值,然后按一个按钮。按下按钮后,将从他们编辑的框中提取信息,然后根据更改所做的更改更新数据库。问题是,textbox.text和checkbox.checked仍然包含最初加载到页面加载子中的值,即使复选框val的文本已被更改。

到目前为止,这是我的代码:

    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

3 个答案:

答案 0 :(得分:7)

你的load语句在每个页面回发时设置表单的值,它可能会覆盖原始值;您需要将foreach代码包装在If (Not Page.IsPostBack) Then .. End If语句中。

HTH

答案 1 :(得分:3)

将页面加载代码放入if(!Page.IsPostback)

答案 2 :(得分:1)

您必须尝试进入页面加载

If (Not Page.IsPostBack) Then 
.. 
End

为了在发布数据时不刷新