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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting mutiple item selections for Listbox 1

Status
Not open for further replies.
Nov 29, 2001
72
0
0
US
Guys,
I'm trying to load a listbox with database values and set multiple select values at the same time.

For instance, I select an administrative assistant from a drop down box which kicks off the selected index change event which grabs the selected value of the drop down box and reposts the page. As the page reloads, I am taking the selected value and reading a doctor table for doctor records with that admin. assistant value and saving the doctor IDs in a comma delimited array.

At the point where the doctor listbox is displayed, I want to list all the doctors in the table and have the appropriate doctors selected according to the admin assistant selected from the aforementioned drop down box.

Here's a code snippet that simply displays all the doctors from the doctor table to the listbox. You can see the doctor list and where I have it in an array but I don't know where to go from here. How do I code the listbox to highlight the doctors associated with the admin assistant selected from the drop down box?


wrkarry = Split(docList, ",")
phyBound = UBound(wrkarry)
indx = 0
cmd = New SqlCommand( _
"SELECT DrID, DrInit FROM PhyDoctor " _
& "ORDER BY DrInit", cnn)
cnn.Open()
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While rdr.Read
lbPhy.Items.Add(New ListItem(rdr "DrInit"), rdr("DrID")))
End While
rdr.Close()

Thanks in advance for your help. It's definitely frustrating going from ASP to ASP.net with a lack of training materials and documentation on what I would consider basic language techniques. This is why I am a member.

-Dave
 
You'll have to do a loop through the entire listbox...

for each item in the listbox

if item in (group of connections) then
highlight roe
next item.




something along these lines.
 
checkai,
Yeah, that's whay I'm trying to do, however I do not know the proper syntax to code it. .NET tends to be very picky about how you code everything. That's what I was looking for.

Thanks for your help,
Dave
 
Dave --

There's probably 3 or 4 ways to accomplish this; below is a small example I put together using a Contacts.mdb I have, it gets the job done.

Code:
<%@ Page Language="VB"%>
<%@Import Namespace = "Microsoft.VisualBasic"%>
<%@Import Namespace = "System"%>
<%@Import Namespace = "System.Web"%>
<%@Import Namespace = "System.Web.UI"%>
<%@Import Namespace = "System.Web.UI.WebControls"%>
<%@Import Namespace = "System.Web.UI.HtmlControls"%>
<%@Import Namespace = "System.Data"%>
<%@Import Namespace = "System.Data.OleDb"%>
<script runat="server">
Private Sub Page_Load(sender As Object, e As EventArgs)
      Dim i As Integer
      If Not IsPostBack Then
           Dim dbconn As OleDbConnection = New OleDbConnection( _
            "Provider=Microsoft.Jet.OLEDB.4.0; " & _
            "Data Source=" & Server.MapPath("fpdb\Contacts.mdb;"))
          Dim DBCommand = New OleDbCommand _
          ("SELECT Contact_ID, ContactName, Newsletter FROM Contacts ORDER BY Contact_ID", dbconn)
          Dim reader As OleDbDataReader          
        Try
         dbconn.Open()
         reader = DBCommand.ExecuteReader(CommandBehavior.CloseConnection)
         Do While reader.Read()
           Dim NewItem As New ListItem()
           NewItem.Value = reader("Contact_ID")
           NewItem.Text = reader("ContactName")
           lbContacts.Items.Add(NewItem)  
           If reader("Newsletter") = true  'check box, want to select all that are checked...
             lbContacts.Items(i).Selected = true
           End If     
            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 If
End Sub
</script>
<HTML>
<HEAD>
<TITLE>Test List box multiple selection</TITLE>
</HEAD>
<body>
<form id="Form1" runat="server">
<asp:Listbox id="lbContacts" runat="server" width="175px" heigh="300px" SelectionMode="Multiple"/>
<asp:Label id="lblResults" runat="server"/>
</form>
</BODY>
</HTML>
 
Thanks Isadore. It looks like something that would work in my situation. I'll give it a shot.
-Dave
 
Got it to work okay. Here's the code snippet:

wrkarry = Split(docList, ",")
phyBound = UBound(wrkarry)
indx1 = 0
cmd = New SqlCommand( _
"SELECT DrID, DrInit FROM PhyDoctor " _
& "ORDER BY DrInit", cnn)
cnn.Open()
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While rdr.Read
lbAvailPhy.Items.Add(New ListItem(rdr("DrInit"), rdr("DrID")))
phyID = rdr("DrID")
indx = 0
Do While indx <= phyBound
wrk = wrkarry(indx)
If phyID = wrk Then
lbAvailPhy.Items(indx1).Selected = True
End If
indx = indx + 1
Loop
indx1 = indx1 + 1
End While
rdr.Close()

Thanks again Isadore.
-Dave
 
Great piece of code Dave - will stick it away in the dot NET files -- good stuff!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top