Setting Selected Values from SqlDataSource

10 posts, 0 answers
  1. Joseph
    Joseph avatar
    29 posts
    Member since:
    Sep 2007

    Posted 20 Jan 2010 Link to this post

    I have a SqlDataSource (Regions_SqlDataSource) populating the RadListBox.
    I have another SqlDataSource (SelectedRegions_SqlDataSource) that gets the selected values from the database.

    I cannot seem to figure out how to set the selected values of the RadListBox based on the SelectedRegions_SqlDataSource.
    I can get it to select the first returned value of the SelectedRegions_SqlDataSource, but it does not select all of the returned values.

    Here is my 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 

    And here is the listbox and data sources.

    <telerik:RadListBox ID="Regions_RadListBox" runat="server" Width="200px"   
          SelectionMode="Multiple" Rows="3" TextMode="MultiLine" DataSortField="Region"   
          DataSourceID="Regions_SqlDataSource1" DataTextField="Region"   
          DataValueField="RegionID" Height="60px">  
    </telerik:RadListBox> 
    <asp:SqlDataSource ID="Regions_SqlDataSource1" 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> 

  2. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1600 posts

    Posted 22 Jan 2010 Link to this post

    Hello Joseph,

    Are you using RadListBox for ASP.NET AJAX?

    RadListBox for ASP.NET AJAX does not contain properties such as Rows and TextMode.

    With respect to your question, RadListBox can have only one selected value. This means, that if you have 3 selected items, the SelectedValue property will return the value of the firstselected item. If you want to grab the values of all selected items, you can iterate over the ListItems and grab those that are selected.

    You can use the RadListBox1.FindItemByValue method in order to search for an item with a given value. Then you can use its Selected property to select it.

    Best wishes,
    Genady Sergeev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Joseph
    Joseph avatar
    29 posts
    Member since:
    Sep 2007

    Posted 22 Jan 2010 Link to this post

    The Rows and TextMode was left over from one of the different things I tried. I took it out now thanks.

    The problem that I'm having is that I don't know how to do what you said. I have posted on multiple forums and tried 100 different things to try to iterate through the items in the SqlDataSource and nothing seems to work. As soon as I set the item to "Selected" it gets rid of one of the other items that was selected so that there is only one selected item when the page actually loads. 

    Could you possibly go one step further and actually show me the code for how to do this? Please?
  4. Joshua Holt
    Joshua Holt avatar
    23 posts
    Member since:
    Jan 2010

    Posted 25 Jan 2010 Link to this post

    Hi Joseph,

    All should work fine if you change you initial load code from
    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  

    To:
    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.FindItemByValue(regionsviewSql("PreferenceValue") ).Selected = true  
                End If  
            Next  


    Then on post back, you can grab
    Regions_RadListBox.SelectedItems and loop over them. 

    The slight change there is not setting the SelectedValue, but actually grabbing each item you want to select, and then selecting it. 
    Hope that works for ya!
    Joshua Holt
  5. Joseph
    Joseph avatar
    29 posts
    Member since:
    Sep 2007

    Posted 26 Jan 2010 Link to this post

    "Then on post back, you can grab
    Regions_RadListBox.SelectedItems and loop over them. "

    How exactly would I do this part? That's where I am having a problem. 
  6. Joshua Holt
    Joshua Holt avatar
    23 posts
    Member since:
    Jan 2010

    Posted 26 Jan 2010 Link to this post

    Hi Joseph,
    I have attached an example based on the northwind database.  It is doing pretty much what you are doing.  It lists the employees in a RadCombobox, and all territories in a RadListBox,  when you select an employee it selects all territories they are associated with.  Then when you click "Post Territories" it will load the selected items from the list box to a panel.
    I hope this helps you.
    You can download the example here: Example 
    Thank you,
    Joshua Holt
  7. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1600 posts

    Posted 26 Jan 2010 Link to this post

    Hello Joseph,

    The easiest way is to use the SelectedItems property of the RadListBox object. By "grab the selected items" I meant the following:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Dim item As Telerik.Web.UI.RadListBoxItem
            For Each item In RadListBox1.Items
                If (item.Selected = True) Then
                    'You can do whatever you want with the selected files
                    Response.Write(item.Value)
                End If
            Next
        End Sub


    Greetings,
    Genady Sergeev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  8. Joseph
    Joseph avatar
    29 posts
    Member since:
    Sep 2007

    Posted 26 Jan 2010 Link to this post

    Thanks Genady, this will help me when I am trying to get the values out of the listbox. I'm still trying to figure out how to actually set the selected items right now, but this will definitely help as soon as I get to his point.

    I'm still trying to figure out what's happening in the example I was given, there's so much going on there and I'm still so new to this it's hard for me to get just the info I need out of such a complex example. 
  9. Joshua Holt
    Joshua Holt avatar
    23 posts
    Member since:
    Jan 2010

    Posted 26 Jan 2010 Link to this post

    Hi Joseph,
    Sorry if the example was a bit complex.  I have simplified it a little, and fully commented it, and you can get the new version here: Clearer Example

    You will mostly just need to look at the Default.aspx.vb file, as the "RadComboBox1_SelectedIndexChanged" change function shows you how to select items in the list box, and the "Button1_Click" function shows you how to loop over the selected items in a listbox.

    Joshua Holt
  10. Joseph
    Joseph avatar
    29 posts
    Member since:
    Sep 2007

    Posted 26 Jan 2010 Link to this post

    Thank you very much, I appreciate all the help.

    I've just been pulled off of this for another project, so I will have to get to it later, but I downloaded the files and have them when I am put back on this. 

    Again, thank you guys for all your help!
Back to Top