Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a listbox in a datagrid that can contain up to two selections. How can I return the selected indexes of the two values? I know how to do it for a listbox that has a selectionmode of "single", but not for multiple.

 

Here is my HTML:

<asp:TemplateColumn HeaderText="Riders">

<ItemTemplate>

<asp:listbox AutoPostBack="False" BackColor="#ffffff" id="lstRider" runat="server" Rows="1" DataSource="<%# DsRider1 %>" Enabled="True" SelectionMode="Multiple" DataTextField="UserName" DataValueField="UserName" SelectedIndex='<%# GetRiderUserName() %>' />

</asp:listbox>

</ItemTemplate>

</asp:TemplateColumn>

 

 

I'm executing a function "GetRiderUserName" in my code-behind to return the selected indices, but don't quite know how to do it. This is as far as I've got:

 

Public Function GetRiderUserName() As Integer

 

'Returns the index of the username

Dim i As Integer

Dim blnFound As Boolean = False

Dim bytFoundCnt As Byte = 0

 

For i = 0 To DsCalDteRider1.CalDteRiders.Rows.Count

If DsCalDteRider1.CalDteRiders.Item(i).Username = DsRider1.Riders.Item(i).Username Then

blnFound = True

If bytFoundCnt = 2 Then 'Max selections allowed

Exit For

End If

Else

blnFound = False

End If

Next

 

If blnFound Then

Return DsRider1.Riders.Item(i).RowNbr

Else

Return -1

End If

 

End Function

 

How can I turn "Return DsRider1.Riders.Item(i).RowNbr" into the selected indexes of a possible two selections?

Thanks,

 

Bill Yeager (MCP, BCIP)

Microsoft Certified Professional

Brainbench Certified Internet Professional, .Net Programmer, Computer Programmer

YeagerTech Consulting, Inc.

Posted

I figured it out. However, there is one thing I'm confused about though. The following statement I have for my other listbox in the DataGrid1_ItemDataBound event which is a single selection listbox:

 

lstCrewChiefTemp.SelectedIndex = lstCrewChiefTemp.Items.IndexOf(lstCrewChiefTemp.Items.FindByValue(e.Item.DataItem("Username")))

 

The above statement automatically selects the value in the listbox without apparantly comparing it to anything else to see if it matches (like I did in the multiple listbox - following example): Note: the "Username" is the DataValueField for the listbox and I have a datasource associated with it: DsCrewChief1

 

For i = 0 To DsCalDteRider1.CalDteRiders.Rows.Count

If DsCalDteRider1.CalDteRiders.Item(i).Username = DsRider1.Riders.Item(i).Username Then

strUsername = DsRider1.Riders.Item(i).Username

lstRiderTemp.Items.FindByValue(strUsername).Selected = True

bytFoundCnt += bytFoundCnt

If bytFoundCnt = 2 Then 'Max selections allowed

Exit For

End If

End If

Next

 

How does this statement work correctly in selecting the proper listitems???

 

lstCrewChiefTemp.SelectedIndex = lstCrewChiefTemp.Items.IndexOf(lstCrewChiefTemp.Items.FindByValue(e.Item.DataItem("Username")))

Thanks,

 

Bill Yeager (MCP, BCIP)

Microsoft Certified Professional

Brainbench Certified Internet Professional, .Net Programmer, Computer Programmer

YeagerTech Consulting, Inc.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...