5

我有一个带有表单的 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
4

3 回答 3

7

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

高温高压

于 2012-09-07T19:34:55.240 回答
3

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

于 2012-09-07T19:34:53.630 回答
1

您必须尝试进入您的页面加载,

If (Not Page.IsPostBack) Then 
.. 
End

为了在您发布数据时不刷新

于 2012-09-07T19:37:57.730 回答