Visual Basic Webform ASP.net

时间:2014-06-10 13:02:28

标签: asp.net vb.net

不确定为什么标签不会连续切换。

我突然指出它,它显示计数器每次都重置,所以它应该是 不断交换数字,但它似乎没有工作

谢谢!

Public Class WebForm2 Inherits System.Web.UI.Page

Dim d As Integer() = {0, 1, 2, 3, 4}
Dim counter As Integer
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Label1.Text = d(0)
        Label2.Text = d(1)
        Label3.Text = d(2)
        Label4.Text = d(3)
        Label5.Text = d(4)
    End If
End Sub

Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim hold As Integer
    counter = 0
    hold = d(0)
    Do While counter < 4
        d(counter) = d(counter + 1)
        counter += 1
    Loop
    Label1.Text = d(0)
    Label2.Text = d(1)
    Label3.Text = d(2)
    Label4.Text = d(3)
    Label5.Text = hold
End Sub

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    Dim hold As Integer
    counter = 0
    hold = d(0)
    Do While counter < 4
        d(counter) = d(counter + 1)
        counter += 1
    Loop

    Label1.Text = d(0)
    Label2.Text = d(1)
    Label3.Text = d(2)
    Label4.Text = d(3)
    Label5.Text = hold
End Sub

End Class

2 个答案:

答案 0 :(得分:1)

我对vb.net并不熟悉,但之前我已经创建了asp.net页面,所以我在这里做了一个有根据的猜测^^

单击按钮时会发生回发并重新加载整个页面,并再次声明数组d。

在Post-Back之后,您存储在数组中的值将丢失。为了保存数据,您可以使用Session或ViewState变量:

Session

ViewState

正如我所说,这是针对asp.net的,但无论如何它都可能有所帮助。

编辑:我仍然认为数据在这个d阵列中丢失了......如果你只是遗漏了数组并且你的button_Click事件是这样的:

        String temp = Label1.Text;

        Label1.Text = Label2.Text;
        Label2.Text = Label3.Text;
        Label3.Text = Label4.Text;
        Label4.Text = Label5.Text;
        Label5.Text = temp;

答案 1 :(得分:1)

你的数组名为d(有更好的名字吗?)正在每次加载页面时重新声明(看起来应该是这样)。

问题是您不会在每个页面加载时保持调整后的数组值。相反,你每次都像其他人所建议的那样从头开始。

有关在Web表单应用程序中保留状态信息的大量有用信息,请参阅以下URL。

http://msdn.microsoft.com/en-us/library/vstudio/z1hkazw7(v=vs.100).aspx

就代码而言,您可以尝试沿着这些行继续保存当前数组中的值,同时为下次加载进行设置。

Public Class WebForm2 Inherits System.Web.UI.Page

    Private _dValues As Integer(19)
    Private _currentValues As Integer(19)
    Private _newValues As Integer(19)
    Private _startIndex As Integer

    Public Sub New()
        _dValues = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
        _startIndex = 0
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If Page.IsPostBack Then 'Load values from form
            For i As Integer = 0 To _currentValues.Length - 1 Step 1
                _currentValues(i) = Request.Form(String.Concat("Hidden", i))
            Next
            _startIndex = Request.Form("StartIndex")
        Else
            _currentValues = _dValues 'First time around
        End If
        Dim position As Integer = _startIndex
        For i As Integer = 0 To _currentvalues.Length - 1 Step 1
            If position >= _newValues.Length Then
                position = 0
            End If
            'Assign the current position in the new array equal to the current sequential value in the previous array
            _newValues(position) = _currentValues(i)
            position += 1
        Next
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Increment the counter every post back
        If Page.IsPostBack Then
            _startIndex += 1
        End If

        'Don't allow the counter to go outside the bounds of the array
        If _startIndex >= _currentValues.Length Then
            _startIndex = 0
        End If
        Me.StartIndex.Text = _startIndex 'Assign the value of the hidden field
    End Sub

    Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    'Dynamically set label / hidden data
        For i As Integer = 0 To _newValues.Length - 1 Step 1
            CType(Page.FindControl(String.Concat("Label", i), Label).Text = _currentValues(i)
            CType(Page.FindControl(String.Concat("Hidden", i), Hidden).Text = _newValues(i)
        Next
    End Sub

    Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    'Dynamically set label / hidden data
        For i As Integer = 0 To _newValues.Length - 1 Step 1
            CType(Page.FindControl(String.Concat("Label", i), Label).Text = _currentValues(i)
            CType(Page.FindControl(String.Concat("Hidden", i), Hidden).Text = _newValues(i)
        Next
    End Sub

End Class

然后以你的形式:

<asp:Hidden ID="StartIndex" runat="server" Value="" />
<asp:Label ID="Label1" runat="server" Value="" />
<asp:Hidden ID="Hidden1" runat="server" Value="" />

<asp:Label ID="Label2" runat="server" Value="" />
<asp:Hidden ID="Hidden2" runat="server" Value="" />
etc...

此代码(或至少其中的一个版本)将构建许多标签和隐藏输入字段,用于显示数据和保留数据。每次发布页面时,起始索引都会递增,这将更改新值的起始位置。虽然当前值将基于从隐藏输入回发的内容。