Help. I am trying to develop a content management system. One of the things I am attempting to do is to draw a list of links from the database and display them as radio buttons so that I can select one and move it up and down in the order. Seems to work fine for moving up, but when I move it down the second time, it is moving the top item in the list down one rather than the selected item. Can anyone offer suggestions to clean up the code and make it work, please. I have tried several variations, all with the same result. This is the latest:


VB Code:
  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         If Not IsPostBack Then
  3.             Dim sQuery As String
  4.             Dim connection As New OleDbConnection(myConnString)
  5.             Dim command As OleDbCommand
  6.             Dim i As Integer = 0
  7.             sQuery = "Select * from tblLinks order by intPosition ASC"
  8.  
  9.             connection.Open()
  10.             command = New OleDbCommand(sQuery, connection)
  11.             Dim objReader As OleDbDataReader = command.ExecuteReader
  12.             Dim btnList As New RadioButtonList
  13.             While objReader.Read
  14.                 i = i + 1
  15.                 btnList.Items.Add(New ListItem(objReader.Item("txtValue"), objReader.Item("intPosition")))
  16.             End While
  17.             btnList.ID = "btnList"
  18.             pnlTest.Controls.Add(btnList)
  19.             Session.Add("Buttons", btnList)
  20.             Session.Add("NumLinks", i)
  21.             objReader.Close()
  22.         connection.Close()
  23.         End If
  24.     End Sub
  25.  
  26.     Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
  27.         moveLinks(CInt(Request.Item("btnList").ToString) - 1, True)
  28.     End Sub ' ' btnUp_click
  29.  
  30.     Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
  31.         moveLinks(CInt(Request.Item("btnList").ToString) - 1, False)
  32.     End Sub ' btnDown_click
  33.  
  34.     Private Sub moveLinks(ByVal id As Integer, ByVal up As Boolean)
  35.         Dim i As Integer
  36.         Dim top As Integer
  37.         Dim bottom As Integer
  38.         Dim switchFrom As Integer
  39.         Dim switchTo As Integer
  40.         Dim selected As Integer
  41.         Dim dir As Integer
  42.         Dim btnList As New RadioButtonList
  43.         Dim size As Integer = Session.Item("NumLinks") - 1
  44.  
  45.  
  46.  
  47.         btnList = Session.Item("Buttons")
  48.  
  49.         If (up And id > 0) Or (Not (up) And id < size) Then
  50.  
  51.             Dim newList As New RadioButtonList
  52.             Dim tmp As String
  53.  
  54.             switchTo = id
  55.  
  56.             If up Then
  57.                 top = 0
  58.                 bottom = size
  59.                 dir = 1
  60.                 switchFrom = id - 1
  61.             Else
  62.                 top = size
  63.                 bottom = 0
  64.                 dir = -1
  65.                 switchFrom = id + 1
  66.             End If
  67.             selected = switchFrom
  68.  
  69.             For i = top To bottom Step dir
  70.                 If i = switchFrom Then
  71.                     tmp = btnList.Items(switchTo).Text
  72.                     Add(newList, tmp, switchTo, dir)
  73.                     tmp = btnList.Items(switchFrom).Text
  74.                     Add(newList, tmp, switchFrom, dir)
  75.                     i = i + dir
  76.                 Else
  77.                     tmp = btnList.Items(i).Text
  78.                     Add(newList, tmp, i, dir)
  79.                 End If
  80.             Next
  81.  
  82.             pnlTest.Controls.Clear()
  83.  
  84.             newList.Items(selected).Selected = True
  85.             newList.ID = "btnList"
  86.             pnlTest.Controls.Add(newList)
  87.  
  88.             Session.Remove("Buttons")
  89.             Session.Add("Buttons", newList)
  90.             newList.Dispose()
  91.         End If
  92.  
  93.     End Sub
  94.  
  95.     Private Sub Add(ByRef tmp As RadioButtonList, ByVal txt As String, ByVal num As Integer, ByVal dir As Integer)
  96.         If dir > 0 Then
  97.             tmp.Items.Add(New ListItem(txt, num))
  98.         Else
  99.             tmp.Items.Insert(0, New ListItem(txt, num))
  100.         End If
  101.     End Sub