This is a migrated thread and some comments may be shown as answers.

Multiple selection with checkboxes

3 Answers 464 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Zoli
Top achievements
Rank 1
Zoli asked on 07 Dec 2010, 05:30 PM

Hello,

I would like to handle multiselection in RadGrid on server side, but not by storing the selected item during every selection/deselection like in the sample http://www.telerik.com/help/aspnet-ajax/grdpersistselectedrowsonsorting.html, but storing them only during sorting, paging, filtering and some custom commands. I have added a GridClientSelectColumn to my MasterTableView columns, so the graphical handling of the multiselection on one page works fine. But if I would like to get in the OnItemCommand handler of the grid(after paging, sorting or RadGrid.DeleteSelectedCommandName), whether the checkbox of a row(DataItem) is checked or not, I can not get this information. The returned states of the checkboxes are always false. Could you suggest a way, how can I get/set the selected state of a row in this case?

I don’t want to have callbacks by every (de)selection, although no EnablePostBackOnRowClick=true and RadGrid.SelectCommandName/RadGrid.DeselectCommandName or GridTemplateColumn with AutoPostBack=true. I could not solve the problem with GridTemplateColumn and AutoPostBack=false and another problem of this case, that the graphical part of the selection is not automatically solved.

Thanks:

Zoltán

My checkbox is in the third column. I get the state of the checkbox with the following:

bool selected = (dataItem.Cells[2].Controls[0] as CheckBox).Checked;

 

My RadGrid definition:

 

    <telerik:RadGrid ID="RadGridFiltered" runat="server" Skin="WebBlue"

                    AllowSorting="true" AllowPaging="true" AllowMultiRowSelection="true" PageSize = 10

                    OnItemCommand="RadGridFiltered_OnItemCommand" OnPreRender="RadGridFiltered_PreRender">

         <MasterTableView DataKeyNames="RETURN_ID" AutoGenerateColumns="true" CommandItemDisplay="Top" >

                <CommandItemTemplate>

                    <div style="padding: 5px 5px;">

                        <asp:LinkButton ID="btnEditSelected" runat="server" CommandName="EditSelected" Visible='<%# RadGridFiltered.EditIndexes.Count == 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/Edit.gif" />Kijelölt módosítása</asp:LinkButton>&nbsp;&nbsp;

                    </div>

                </CommandItemTemplate>

                <Columns>                                  

                    <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />                   

                </Columns>

        </MasterTableView>

        <ClientSettings EnableRowHoverStyle="true">

            <Selecting AllowRowSelect="true" EnableDragToSelectRows="true" />

       </ClientSettings>

    </telerik:RadGrid>

 

 

My selection store handler:

    public void RadGridFiltered_OnItemCommand(object sender, GridCommandEventArgs args)
    {
        StoreSelectedItems();

       .........

    }


    const string SessionSelectedItems = "SelectedItems";

    const string KeyName = "RETURN_ID";


    protected void StoreSelectedItems()

    {

        ArrayList selectedItems = (Session[SessionSelectedItems] != null) ? (ArrayList)(Session[SessionSelectedItems]) : new ArrayList();


        foreach (GridDataItem dataItem in RadGridFiltered.MasterTableView.Items)

        {

            bool selected = (dataItem.Cells[2].Controls[0] as CheckBox).Checked;     //  ALWAYS FALSE !!!

            dataItem.Selected = selected;

            string id = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex][KeyName].ToString();

 

            if (selected && !selectedItems.Contains(id))

                selectedItems.Add(id);

            else if (!selected && selectedItems.Contains(id))

                selectedItems.Remove(id);

        }

 

        Session[SessionSelectedItems] = selectedItems;

    }

3 Answers, 1 is accepted

Sort by
0
dhuss
Top achievements
Rank 1
answered on 09 Dec 2010, 06:05 PM
It doesn't look like you are filtering on the commandname for your linkbutton. Check the commandname when your onitemcommand fires. There are numerous commandnames that aren't seen, example "Page". You need to run your code for your specfic commandname.

If args.commandname = "EditSelected" then
    StoreSelectedItems()
end if
0
Zoli
Top achievements
Rank 1
answered on 10 Dec 2010, 07:59 AM
My question is independent from command names. I know and use the command names, but think, they have nothing to do with my problem. See my description!
0
Tsvetina
Telerik team
answered on 10 Dec 2010, 01:24 PM
Hello Zoltán,

Since RadGrid rebinds on ItemCommand, it does not persist its selected state afterwards. You can see this even in the online demo on server-side selection in RadGrid. However, you can manually persist the selected state of the control following the instructions from this help article.

Regards,
Tsvetina
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Zoli
Top achievements
Rank 1
Answers by
dhuss
Top achievements
Rank 1
Zoli
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or