Set Selected Listbox Items From SqlDataSource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmmitw
    New Member
    • Jan 2010
    • 4

    Set Selected Listbox Items From SqlDataSource

    I have a multiselect enabled listbox on the page that is populated from a SqlDataSource.

    I have another DataSource that retrieves the selected items from the database.

    I cannot for the life of me figure out how to set all of the selected items in the listbox.

    I have figured out how to get the first item from the selected items sqldatasource, but I don't know how to set ALL of the items from the sqldatasource as selected.

    Any help would be greatly appreciated. I have been working on this for 3 days now to no avail.

    Here is my listbox, sqlqueries, and code:

    Code:
    <telerik:RadListBox ID="Regions_RadListBox" runat="server" Width="200px" 
                 SelectionMode="Multiple" Rows="3" TextMode="MultiLine" DataSortField="Region" 
                 DataSourceID="Regions_SqlDataSource" DataTextField="Region" 
                 DataValueField="RegionID" Height="60px">
    </telerik:RadListBox>

    Code:
    <asp:SqlDataSource ID="Regions_SqlDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>" 
        SelectCommand="SELECT [RegionID], [Region] FROM [NC_CT_Regions]"></asp:SqlDataSource>
    
    <asp:SqlDataSource ID="SelectedRegions_SqlDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>" 
        SelectCommand="SELECT PreferenceValue FROM NC_EmployeePreference WHERE PreferenceTypeID = '4' AND EmpID = @EmpID">
        <SelectParameters>
            <asp:QueryStringParameter Name="EmpID" QueryStringField="EmpID" />
        </SelectParameters>
    </asp:SqlDataSource>

    Code:
    Dim regionsSql As DataView = DirectCast(SelectedRegions_SqlDataSource.Select(DataSourceSelectArguments.Empty), DataView)
            For Each regionsviewSql As DataRowView In regionsSql
                If Not Page.IsPostBack And regionsviewSql("PreferenceValue").ToString <> "" Then
                    Regions_RadListBox.SelectedValue = regionsviewSql("PreferenceValue")
                End If
            Next
    Last edited by Frinavale; Jan 21 '10, 07:47 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • marcellus7
    New Member
    • Oct 2008
    • 33

    #2
    For the line in your if statement, try selecting this way:

    Regions_RadList Box.Items(Regio ns_RadListBox.I tems.IndexOf(re gionsviewSql("P referenceValue" ))).Selected = True

    That way it just finds that item in the listbox, and selects it.

    Comment

    • cmmitw
      New Member
      • Jan 2010
      • 4

      #3
      It tells me:

      'Telerik.Web.UI .ControlItemCol lection.Protect ed Friend Overridable Function IndexOf(item As Telerik.Web.UI. ControlItem) As Integer' is not accessible in this context because it is 'Protected Friend'.

      and hilights this section of code:

      Regions_RadList Box.Items.Index Of

      Comment

      • marcellus7
        New Member
        • Oct 2008
        • 33

        #4
        This may not be the most efficient way, but you can try this in your if

        For each item as Telerik.Web.UI. ControlItem In Regions_RadList Box.Items
        If item.ToString() = regionsviewSql( "PreferenceValu e")
        item.selected = True
        End If
        Next

        Comment

        • cmmitw
          New Member
          • Jan 2010
          • 4

          #5
          Apparently:

          'Selected' is not a member of 'Telerik.Web.UI .ControlItem'

          I tried 'ListItem' instead, and the error went away, but nothing got selected in the box.

          I appreciate your help on this!

          Comment

          Working...