This is a migrated thread and some comments may be shown as answers.
Retrieve SelectedItems
8 Answers 117 Views
This is a migrated thread and some comments may be shown as answers.
Barnabas
Top achievements
Rank 1
Barnabas asked on 28 Oct 2009, 02:07 PM
Hello,

I am using the strategy put forth in Persisting the selected rows client-side on sorting/paging/filtering/grouping. This works well for tracking selected items across paging. After the user is done selecting all their items they select an action, such as delete. When they do this the
grid.SelectedItems 
collection only contains the items on the current page. How can I get at the full list of selected items from all pages. Shouldn't the
grid.SelectedItems 
collection be returning the whole list from the javascript variable
var selected = {}; 

8 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 28 Oct 2009, 02:21 PM
Hi Barnabas,

Indeed the SelectedItems collection will contain the items on the active grid page only. If you want to delete all selected records in the data source, you will need to pass the selected javascript array on server and build your delete logic based on the items contained in that array.

Best regards,
Sebastian
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.
0
Barnabas
Top achievements
Rank 1
answered on 28 Oct 2009, 02:59 PM
May I ask what the purpose of persisting selected items is if I can't access the subsequent list? Or is it just available client side and not server side. I could be missing something.

Essentially what I am trying to do is the same as Deleting grid items depending on GridCheckBoxColumn values or checkbox persistence in GridTemplateColumn, but instead do the persistence client side.


0
Sebastian
Telerik team
answered on 28 Oct 2009, 04:41 PM
Hello Barnabas,

The SelectedItems RadGrid collection contains the items from the active page (the same is true for the regular MS GridView control). To achieve your goal, you will need to follow the instructions from my previous post.

Let me know if you experience difficulties during the implementation process.
 
Best regards,
Sebastian
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.
0
Barnabas
Top achievements
Rank 1
answered on 28 Oct 2009, 04:45 PM
Sebastian,

Thanks so much. I really appreciate your help. I may be asking an elementary question, but how would I go about "pass the selected javascript array on server?" I know how to set one javascript value to a hidden field and then retrieve that on PostBack. How would I go about doing the same with an array (that could potentially contain thousands of integer values). Am I going in the right direction?

Barnabas
0
Sebastian
Telerik team
answered on 29 Oct 2009, 10:58 AM
Hello Barnabas,

One possible solution would be to initiate ajax request from the client using the ajaxRequest(args) method of RadAjaxManager as demonstrated here (passing the array in the args parameter). Then parse the values on the server inside the OnAjaxRequest handler extracting them from e.Argument in order to determine the selected grid items.

Best regards,
Sebastian
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.
0
dchymko
Top achievements
Rank 1
answered on 26 Nov 2009, 06:11 PM
Instead of passing the array through as AJAX I've been trying to copy the selected  into a hidden field with no luck as it doesn't seem to be a regular array, just an object.  The selected javascript array I'm referring to is here:
http://www.telerik.com/help/aspnet-ajax/grid-persist-selected-rows-client-sorting-paging-grouping-filtering.html

I basically have the requirement to allow multiple rows to be selected with the selection being maintained across postbacks and paging.  I had a look at this example:

http://demos.telerik.com/aspnet-ajax/grid/examples/programming/selectrowwithcheckbox/defaultcs.aspx

But it doesn't maintain the checkbox selection across page changes. What is the best way to set the grid up with a checkbox mutliselect so that I can get all of the items when a user clicks a postback button on the page?

Daryl








0
Sebastian
Telerik team
answered on 27 Nov 2009, 08:18 AM
Hello dchymko,

Based on the description you provided, I can offer you either to:

In case you would like to use checkbox instead of select/deselect buttons in the grid, you will need to alter the logic from the second resource to make it operate with checkboxes used for row selection.

Best regards,
Sebastian
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.
0
Barnabas
Top achievements
Rank 1
answered on 01 Dec 2009, 05:36 PM
Daryl,

I decided to stick with client side selection. I felt like the performance was much snappier for larger lists. (I have a requirement to display thousands of items at a time.)

In order to persist the selected items collection, I used a technique that I read about on this page.

There are lots of ways to do it, but this one worked for me. Here is what my code ended up looking like.

.aspx

 function moveVariableToHiddenInput() { 
            // Set the value of the hidden variable to 
            // the value of the javascript variable 
            var hiddenControl = '<%= selectedInstrumentIds.ClientID %>'
            document.getElementById(hiddenControl).value = Sys.Serialization.JavaScriptSerializer.serialize(selected); 
        } 
 <asp:Button ID="DeleteInstruments" runat="server" OnClick="DeleteInstruments_Click" 
            Text="Delete Selected Instruments" OnClientClick="moveVariableToHiddenInput()" /> 
        <input id="selectedInstrumentIds" type="hidden" runat="server" /> 




.aspx.cs

var serializedValues = selectedInstrumentIds.Value; 
            JavaScriptSerializer serializer = new JavaScriptSerializer(); 
            var jsIds = serializer.Deserialize<Dictionary<stringobject>>(serializedValues); 
 
            var selectedItems = new HashSet<long>(); 
            foreach (KeyValuePair<stringobject> keyValuePair in jsIds) 
                selectedItems.Add(long.Parse(keyValuePair.Key)); 
 
            SelectedItems = selectedItems; 

I then have SelectedItems as a property that I can use like so.

 protected void _Grid_PreRender(object sender, EventArgs e) 
        { 
            if (!DeleteConfirmationMode) return
 
            foreach (GridDataItem item in _Grid.MasterTableView.Items) 
            { 
                object value = item.OwnerTableView.DataKeyValues[item.ItemIndex]["Id"]; 
                if (!SelectedItems.Contains((long)value)) 
                    item.Visible = false
            } 
        } 

So I only filter if the user is confirming that they are deleting the selected items. I think that should be enough to get you on your way.

Barnabas









Tags
Grid
Asked by
Barnabas
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Barnabas
Top achievements
Rank 1
dchymko
Top achievements
Rank 1
Share this question
or