Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim sQuery As String
Dim connection As New OleDbConnection(myConnString)
Dim command As OleDbCommand
Dim i As Integer = 0
sQuery = "Select * from tblLinks order by intPosition ASC"
connection.Open()
command = New OleDbCommand(sQuery, connection)
Dim objReader As OleDbDataReader = command.ExecuteReader
Dim btnList As New RadioButtonList
While objReader.Read
i = i + 1
btnList.Items.Add(New ListItem(objReader.Item("txtValue"), objReader.Item("intPosition")))
End While
btnList.ID = "btnList"
pnlTest.Controls.Add(btnList)
Session.Add("Buttons", btnList)
Session.Add("NumLinks", i)
objReader.Close()
connection.Close()
End If
End Sub
Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
moveLinks(CInt(Request.Item("btnList").ToString) - 1, True)
End Sub ' ' btnUp_click
Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
moveLinks(CInt(Request.Item("btnList").ToString) - 1, False)
End Sub ' btnDown_click
Private Sub moveLinks(ByVal id As Integer, ByVal up As Boolean)
Dim i As Integer
Dim top As Integer
Dim bottom As Integer
Dim switchFrom As Integer
Dim switchTo As Integer
Dim selected As Integer
Dim dir As Integer
Dim btnList As New RadioButtonList
Dim size As Integer = Session.Item("NumLinks") - 1
btnList = Session.Item("Buttons")
If (up And id > 0) Or (Not (up) And id < size) Then
Dim newList As New RadioButtonList
Dim tmp As String
switchTo = id
If up Then
top = 0
bottom = size
dir = 1
switchFrom = id - 1
Else
top = size
bottom = 0
dir = -1
switchFrom = id + 1
End If
selected = switchFrom
For i = top To bottom Step dir
If i = switchFrom Then
tmp = btnList.Items(switchTo).Text
Add(newList, tmp, switchTo, dir)
tmp = btnList.Items(switchFrom).Text
Add(newList, tmp, switchFrom, dir)
i = i + dir
Else
tmp = btnList.Items(i).Text
Add(newList, tmp, i, dir)
End If
Next
pnlTest.Controls.Clear()
newList.Items(selected).Selected = True
newList.ID = "btnList"
pnlTest.Controls.Add(newList)
Session.Remove("Buttons")
Session.Add("Buttons", newList)
newList.Dispose()
End If
End Sub
Private Sub Add(ByRef tmp As RadioButtonList, ByVal txt As String, ByVal num As Integer, ByVal dir As Integer)
If dir > 0 Then
tmp.Items.Add(New ListItem(txt, num))
Else
tmp.Items.Insert(0, New ListItem(txt, num))
End If
End Sub