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

Linking a checkbox choice w/ filling a listbox 1

Status
Not open for further replies.

JoeCool32

Programmer
Sep 26, 2002
50
US
I have two checkboxes. Depending on which one I check, a listbox will be filled with the appropriate data. I haven't been able to get it to work with the code I have, which is as follows:

.aspx file
Code:
<TR>
	<TD><ASP:CHECKBOX autopostback=&quot;True&quot; id=&quot;chkCurrent&quot; runat=&quot;server&quot; text=&quot;Users Currently Logged In&quot;></ASP:CHECKBOX> </TD>
	<TD><ASP:CHECKBOX autopostback=&quot;True&quot; id=&quot;chkAll&quot; runat=&quot;server&quot; text=&quot;All Users Permitted Access&quot;></ASP:CHECKBOX></TD>
</TR>
<TR>
	<TD><ASP:LINKBUTTON id=&quot;lkbtnSelect&quot; runat=&quot;server&quot;>Select All</ASP:LINKBUTTON></TD>
	<TD colspan=&quot;2&quot;><ASP:LINKBUTTON id=&quot;lkbtnUnselect&quot; runat=&quot;server&quot;>Unselect All</ASP:LINKBUTTON></TD>
</TR>
<TR>
	<TD><ASP:LISTBOX autopostback=&quot;True&quot; id=&quot;lstUsers&quot; rows=&quot;10&quot; runat=&quot;server&quot; selectionmode=&quot;Multiple&quot; width=&quot;200px&quot;></ASP:LISTBOX></TD>
	<TD align=&quot;middle&quot;><ASP:BUTTON id=&quot;btnAdd&quot; onclick=&quot;AddToListClick&quot; runat=&quot;server&quot; text=&quot;Add &#8594;&quot;></ASP:BUTTON><BR><BR><BR>
	<ASP:BUTTON id=&quot;btnRemove&quot; onclick=&quot;RemoveFrListClick&quot; runat=&quot;server&quot; text=&quot;&#8592; Remove&quot;></ASP:BUTTON></TD>
	<TD><ASP:LISTBOX autopostback=&quot;True&quot; id=&quot;lstSelectUser&quot; rows=&quot;10&quot; runat=&quot;server&quot; selectionmode=&quot;Single&quot; width=&quot;200px&quot;></ASP:LISTBOX></TD>
</TR>
.vb file

Sub SelectNumberOfUsers()
If chkCurrent.Checked = True Then
lstUsers.Items.Add(&quot;Item 1&quot;)
ElseIf chkAll.Checked = True Then
Dim intFirst As Integer
For intFirst = 1 To 10
lstUsers.Items.Add(&quot;Item &quot; & intFirst)
Next
End If
End Sub


Essentially the listbox won't populate at all regardless of which box I check. >:-< Not sure what I'm doing wrong. Any ideas? JJ [peace]

&quot;Ignorance and prejudice and fear walk hand in hand&quot; - Witch Hunt, by Rush
 
JJ: Not sure how you're referencing your subroutine. I put the routine in a Page_Load event and it worked; i.e.,

Sub Page_Load
SelectNumberOfUsers()
End Sub

Sub SelectNumberOfUsers()
If chkCurrent.Checked = True Then
lstUsers.Items.Add(&quot;Item 1&quot;)
ElseIf chkAll.Checked = True Then
Dim intFirst As Integer
For intFirst = 1 To 10
lstUsers.Items.Add(&quot;Item &quot; & intFirst)
Next
End If
End Sub

 
Well, yes, it does work if it's in the Page_Load event, but I've got another routine in that event. I want the checkbox sub to work outside of it.

My program's Page_Load takes data from a SQL table and fills a ddl w/ it. Based on the ddl selection the checkboxes are to appear, then based on the checkbox selection the listbox appears w/ the accompanying data.

JJ [peace]

&quot;Ignorance and prejudice and fear walk hand in hand&quot; - Witch Hunt, by Rush
 
Here's how I'm actually referencing the subroutine. There's a lot of code here, so I'll hilite where it is.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
SQLSearchAndDdlFill()
If Page.IsPostBack Then
ddlApplications_SelectedIndexChanged(sender, e)
End If
End Sub

Sub SQLSearchAndDdlFill()
'pulling application names from umv_applications table
Dim objSQLAppName As New SQLComponent()
objSQLAppName.strConnection = objConstants.DBConnectionString_Umove
objSQLAppName.strSQL = &quot;SELECT * FROM umv_applications ORDER BY app_applicationName ASC&quot;
objSQLAppName.ExecuteSqlStatementAndReturnResults()
If objSQLAppName.returnDataViewValue.Count > 0 Then
'adds each name to drop down list & makes list visible
Dim strAppName As String = &quot;&quot;
Dim intDropDownCounter As Integer = 0
Dim intCounter As Integer
For intCounter = 0 To objSQLAppName.returnDataViewValue.Count - 1
ddlApplications.Items.Add(Trim (objSQLAppName.returnDataViewValue(intCounter)(&quot;app_applicationName&quot;).ToString()))
intDropDownCounter += 1
strAppName = Trim(objSQLAppName.returnDataViewValue(intCounter)(&quot;app_applicationName&quot;).ToString())
Next
ddlApplications.Visible = True
Else
'makes drop down list not visible
ddlApplications.Visible = False
End If
End Sub

Public Sub ddlApplications_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlApplications.SelectedIndexChanged
If Trim(ddlApplications.SelectedItem.Text) <> &quot;&quot; Then
chkCurrent.Visible = True
chkAll.Visible = True

SelectNumberOfUsers()
Else
chkCurrent.Visible = False
chkAll.Visible = False
End If
End Sub
JJ [peace]

&quot;Ignorance and prejudice and fear walk hand in hand&quot; - Witch Hunt, by Rush
 
[tt]
Public Sub ddlApplications_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlApplications.SelectedIndexChanged
If Trim(ddlApplications.SelectedItem.Text) <> &quot;&quot; Then
chkCurrent.Visible = True
chkAll.Visible = True
SelectNumberOfUsers()
Else
chkCurrent.Visible = False
chkAll.Visible = False
End If
End Sub
[/tt]


ddlApplications.SelectedItem returns an Object (should be castable to a String in this case), which does not have a Text property.

What you probably want for the test to ensure that something is selected is:
[tt]If (Not ddlApplications.SelectedItem Is Nothing) Then[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top