Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ordered Lists with CheckBoxList

Status
Not open for further replies.

Sheffield

Programmer
Jun 1, 2001
180
US
Greetings,

Can someone please clue me in as to how I can assign line numbers (ordered list) to a CheckBoxList?

Somehow, I am unsure if this is a possibility.

Anyway, I'm pulling data from an Access db and populating the CheckBoxList with the results. I can do this no problem, but I'd LOVE to be able to assign a line number to each checkbox so its easy to reference a particular checkbox.

Please let me know if you have an alternative method of doing this if you aren't using a CheckBoxList.

Thanks:)
 
Shef: In the following script, you can assign line numbers to a checkbox list and then read the selected values (putting them in a text box as a string). The only thing this routine doesn't do is list the line numbers when the checkboxes are displayed; wasn't sure if you wanted to do that:

Code:
<script runat="server">
  Dim i As Integer = 0
  Private Sub Page_Load(sender As Object, e As EventArgs)    
    If Not IsPostBack Then
      popchkList()
    End If  
   End Sub

   Private Sub popchkList()     
       chkList.Items.Clear() 
       Dim dbconn As OleDbConnection = New OleDbConnection( _
        "Provider=Microsoft.Jet.OLEDB.4.0; " & _
        "Data Source=" & Server.MapPath("fpdb\Sites.mdb;")) 
        Dim DBCommand = New OleDbCommand ("SELECT DISTINCT County FROM WebMasterSites", dbconn)
        Dim reader As OleDbDataReader
        Try
         dbconn.Open()
         reader = DBCommand.ExecuteReader()
         Do While reader.Read()
           Dim NewItem As New ListItem()
           NewItem.Value = i + 1
           NewItem.Text = reader("County")
           chkList.Items.Add(NewItem) 
           i=i+1          
         Loop           
           reader.Close()
           Catch err As Exception
           lblResults.Text = "Error reading list of names. "
           lblResults.Text &= err.Message
         Finally
           If (Not dbconn Is Nothing) Then
             dbconn.Close()
           End If
         End Try  
    End Sub    

    Sub btnLine_Click(sender As Object, e As EventArgs)
       Dim s As String
       Dim r As String
       Dim i As Integer
       Dim n As Integer            
       'Make sure at least one County is selected...or collect the various ones selected...  	
       n = 0
       If chkList.SelectedIndex <> -1 Then          
            For i = chkList.SelectedIndex To chkList.Items.Count - 1
               If chkList.Items(i).Selected Then
                  n = n + 1  
                  If n = 1 Then              
                    r &= ","  & chkList.Items(i).Value 
                  Else       
                    r &= ", " & chkList.Items(i).Value 
                  End If
               End If
            Next
          End If
          LineNo.Text = r
    End Sub    
</script>
 
Shef: good thread up on "Checkboxes and ViewState", should be on Page 1 or 2, might want to take a look.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top