[英]Jquery UI working in Visual Studio 2012 asp.net webform
[英]Visual basic Webform ASP.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
我對vb.net並不熟悉,但是我之前已經創建過asp.net頁面,所以我在這里進行了有根據的猜測^^
單擊該按鈕時,將發生回發,並且整個頁面將被重新加載,並且再次聲明數組d。
回發后,存儲在數組中的值將丟失。 為了保留數據,可以使用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;
您的名為d的數組(是否有更好的名稱?)在每次頁面加載時都重新聲明(看起來應該如此)。
問題在於您不會在每次頁面加載時都保留調整后的數組值。 相反,您每次都是從頭開始,就像其他人所建議的那樣。
有關在Web窗體應用程序中保留狀態信息的許多有用信息,請參見以下URL。
http://msdn.microsoft.com/zh-CN/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...
此代碼(或其至少一個版本)將構建許多標簽和隱藏的輸入字段,這些字段將用於顯示數據和保留數據。 每次發布頁面時,起始索引都會增加,這將更改新值的起始位置。 而當前值將基於從隱藏輸入中回發的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.