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

Multiple selection in the Master grid not being retained on PostBack

3 Answers 100 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simba
Top achievements
Rank 1
Simba asked on 30 Aug 2011, 05:03 PM
I have to two Rad Grids functioning as master details linked by this select parameter,  I need to make multiple selection on both grids , the problem I have is the multiple selection in the master grid is not being retained in the detail grid on post back. How can I make multiple selection in the master grid and filter by that multiple selection in the details grid?
the experience I am getting right now is the selection selection is used for the controlparameter value but I need to use all the selected values?
  <SelectParameters>
         <asp:ControlParameter ControlID="RadGrid1"  Name="Region" PropertyName="SelectedValue" Type="String" DefaultValue="All" /> 
   </SelectParameters>  
Thanks

3 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 01 Sep 2011, 11:55 AM
Hello Sirak,

A declarative datasource control would not be able to provide such functionality out of the box. You should better switch to programmatic binding through NeedDataSource for the grid and filter the values for the second grid manually by building a select statement based on the number of selected items and their datakey values.
You could base your server logic on this Code library, it also performs multivalue filtering (which is what you actually need to achieve).

All the best,
Tsvetina
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Simba
Top achievements
Rank 1
answered on 02 Sep 2011, 04:20 PM
Thanks for your response.
   I have move the data binding to the code behind and Configured the Ajax Manager according to the behavior I wanted to see, i.e. when ever the RegionRadGrid checkbox state is changed, it should update the country list in the CountryRadGrid. But for some reason it is not firing RadAjaxManager1_AjaxRequest and hence updating the country grid.

Here is the code snippet, I highly appreciate if you could help...


ASPX Page
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Subscription.aspx.vb" Inherits="_Default" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Register</title>    
    <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" CdnSettings-TelerikCdn="Enabled" /> 
</head>
<body>
    <form id="form1" runat="server">  
          <telerik:RadScriptManager ID="RadScriptManager1" runat="server" CdnSettings-TelerikCdn="Enabled">
                        <Scripts>          
                            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
                            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
                            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
                        </Scripts>
          </telerik:RadScriptManager>          
            
            <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1"
                OnAjaxRequest="RadAjaxManager1_AjaxRequest">
               <AjaxSettings>
                    <telerik:AjaxSetting AjaxControlID="pnlRegionGrid">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="pnlCountryGrid" LoadingPanelID="RadAjaxLoadingPanel1" />                        
                        </UpdatedControls>
                    </telerik:AjaxSetting>                    
               </AjaxSettings>
           </telerik:RadAjaxManager>
          <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1" />        
          <telerik:RadSkinManager ID="RadSkinManager1" runat="server" Skin="Outlook">
          </telerik:RadSkinManager>  
       <div>
           <table>
            
                   <tr> 
                      <asp:Panel id="pnlRegionGrid" Runat="server" Visible="True" BorderStyle="None"> 
                              <td>
                                   <font face="arial,helvetica,sans-serif" size="2"><b>Main Region of Interest:</b></font>
                               </td>  
                     <td>
                          <font face="arial,helvetica,sans-serif" size="2">        
                                   <asp:Literal ID="Literal1" runat="server" />     
                                  <telerik:RadGrid ID="RegionRadGrid" OnNeedDataSource="RegionRadGrid_NeedDataSource" ShowStatusBar="true" runat="server" ShowGroupPanel="True" GridLines="None" AllowMultiRowSelection="true"  Width="500px">
                                    <MasterTableView>
                                        <Columns>
                                            <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
                                                <HeaderTemplate>
                                                    <asp:CheckBox id="headerChkbox" OnCheckedChanged="ToggleSelectedState" AutoPostBack="True" runat="server"></asp:CheckBox>
                                                </HeaderTemplate>                                               
                                                <ItemTemplate>
                                                    <asp:CheckBox id="CheckBox1" OnCheckedChanged="ToggleRowSelection" AutoPostBack="True" runat="server"></asp:CheckBox>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                        </Columns>
                                    </MasterTableView>                                    
                                    <ClientSettings EnableRowHoverStyle="true" />                              
                                                           
                                    </telerik:RadGrid>                                       
                                   </font>         
                      </td>
              </asp:Panel>  
                   </tr>      
          <tr>
            <asp:panel id="pnlCountryGrid" Runat="server" Visible="True">               
                                <td>
                                       <font face="arial,helvetica,sans-serif" size="2"><b>Main Countries of Interest:</b></font>
                                </td>
                                <td>
                                 <telerik:RadGrid ID= CountryRadGrid ShowStatusBar="true"  OnNeedDataSource="CountryRadGrid_NeedDataSource" runat="server" ShowGroupPanel="True" GridLines="None" AllowMultiRowSelection="true"  Width="500px" >
                                    <MasterTableView>
                                        <Columns>
                                            <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn2">
                                                <HeaderTemplate>
                                                    <asp:CheckBox id="headerChkbox2" OnCheckedChanged="ToggleSelectedState" AutoPostBack="True" runat="server"></asp:CheckBox>
                                                </HeaderTemplate>                                               
                                                <ItemTemplate>
                                                    <asp:CheckBox id="CheckBox2" OnCheckedChanged="ToggleRowSelection" AutoPostBack="True" runat="server"></asp:CheckBox>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                        </Columns>
                                    </MasterTableView>
                                    <ClientSettings EnableRowHoverStyle="true" >
                                    <Scrolling AllowScroll="true" />
                                    </ClientSettings>                
                                    </telerik:RadGrid> 
                                  
                                </td>
                      </asp:panel>                    
                   </tr>              
            </table>
    </div>
    </form>
</body>
</html>

Code Behind:

Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data
Imports Telerik.Web.UI
Imports Telerik.Web




Public Class _Default
    Inherits System.Web.UI.Page


    Protected WithEvents TopicRadGrid As Global.Telerik.Web.UI.RadGrid
    Private oConnection As SqlClient.SqlConnection
    Public sb As StringBuilder


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles RegionRadGrid.PreRender
        Literal1.Text = String.Format("<h3 class=""qsfSubtitle"">Selected rows count is: {0}</h3>", RegionRadGrid.SelectedItems.Count)


    End Sub
    Protected Sub RegionRadGrid_NeedDataSource(ByVal sender As Object, ByVal e As EventArgs)
        oConnection = New SqlConnection(ConfigurationManager.AppSettings("ConnString"))
        Dim adapter As SqlDataAdapter = New SqlDataAdapter
        adapter.SelectCommand = New SqlCommand("SELECT [Region]  FROM [IMFEXTRANET_OMBC3].[dbo].[OMBC_Region] WHERE Status='A'", oConnection)
        Dim myDataTable As New DataTable
        oConnection.Open()
        Try
            adapter.Fill(myDataTable)
        Finally
            oConnection.Close()
        End Try
        RegionRadGrid.DataSource = myDataTable
    End Sub
    Protected Sub ToggleRowSelection(ByVal sender As Object, ByVal e As EventArgs)
        CType(CType(sender, CheckBox).NamingContainer, GridItem).Selected = CType(sender, CheckBox).Checked


    End Sub
    Private Sub RegionRadGrid__ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RegionRadGrid.ItemCreated
        If TypeOf e.Item Is GridDataItem Then
            AddHandler e.Item.PreRender, AddressOf RegionRadGrid_ItemPreRender
        End If
    End Sub
    Protected Sub RegionRadGrid_ItemPreRender(ByVal sender As Object, ByVal e As EventArgs)
        CType(CType(sender, GridDataItem)("CheckBoxTemplateColumn").FindControl("CheckBox1"), CheckBox).Checked = CType(sender, GridDataItem).Selected
    End Sub
    Protected Sub ToggleSelectedState(ByVal sender As Object, ByVal e As EventArgs)
        Dim headerCheckBox As CheckBox = CType(sender, CheckBox)
        For Each dataItem As GridDataItem In RegionRadGrid.Items
            CType(dataItem.FindControl("CheckBox1"), CheckBox).Checked = headerCheckBox.Checked
            dataItem.Selected = headerCheckBox.Checked
        Next
    End Sub
    Protected Sub RegionRadGrid_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles RegionRadGrid.PreRender
        Dim headerItem As GridHeaderItem = CType(RegionRadGrid.MasterTableView.GetItems(GridItemType.Header)(0), GridHeaderItem)
        CType(headerItem.FindControl("headerChkbox"), CheckBox).Checked = RegionRadGrid.SelectedItems.Count = RegionRadGrid.Items.Count
    End Sub
    Private Sub CountryRadGrid__ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles CountryRadGrid.ItemCreated
        If TypeOf e.Item Is GridDataItem Then
            AddHandler e.Item.PreRender, AddressOf CountryRadGrid_ItemPreRender
        End If
    End Sub
    Protected Sub CountryRadGrid_ItemPreRender(ByVal sender As Object, ByVal e As EventArgs)
        CType(CType(sender, GridDataItem)("CheckBoxTemplateColumn").FindControl("CheckBox1"), CheckBox).Checked = CType(sender, GridDataItem).Selected
    End Sub
    Protected Sub CountryRadGrid_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs)
        sb = New StringBuilder()
        For Each selectedItem As GridDataItem In RegionRadGrid.SelectedItems
            sb.Append("'")
            sb.Append(selectedItem("Region").Text)
            sb.Append("',")
        Next
        If sb.Length > 0 Then
            sb.Remove(sb.Length - 1, 1)
        End If


        Dim str As String = "SELECT CC.cty_name_formal as CountryName FROM COUNTRYDB.countrydb.dbo.vw_iso_cty AS CC INNER JOIN IMFEXTRANET_OMBC3.dbo.OMBC_CountryRegion AS CR  ON CC.ISO_code = CR.ISOCODE   WHERE CR.RegionID IN (SELECT RegionID FROM dbo.OMBC_Region WHERE Region IN ('" + sb.ToString() + "')) ORDER BY  CC.cty_name_formal"
        CountryRadGrid.DataSource = GetDataTable(str)
    End Sub
    Public Function GetDataTable(ByVal query As String) As DataTable
        oConnection = New SqlConnection(ConfigurationManager.AppSettings("ConnString"))
        Dim adapter As New SqlDataAdapter()
        adapter.SelectCommand = New SqlCommand(query, oConnection)
        Dim myDataTable As New DataTable()
        oConnection.Open()
        Try
            adapter.Fill(myDataTable)
        Finally
            oConnection.Close()
        End Try
        Return myDataTable
    End Function
    Protected Sub RadAjaxManager1_AjaxRequest(ByVal sender As Object, ByVal e As AjaxRequestEventArgs)
        sb = New StringBuilder()
        For Each selectedItem As GridDataItem In RegionRadGrid.SelectedItems
            sb.Append("'")
            sb.Append(selectedItem("Region").Text)
            sb.Append("',")
        Next
        If sb.Length > 0 Then
            sb.Remove(sb.Length - 1, 1)
        End If
        Dim str As String = "SELECT CC.cty_name_formal as CountryName FROM COUNTRYDB.countrydb.dbo.vw_iso_cty AS CC INNER JOIN IMFEXTRANET_OMBC3.dbo.OMBC_CountryRegion AS CR  ON CC.ISO_code = CR.ISOCODE   WHERE CR.RegionID IN (SELECT RegionID FROM dbo.OMBC_Region WHERE Region IN ('" + sb.ToString() + "')) ORDER BY  CC.cty_name_formal"
        CountryRadGrid.DataSource = GetDataTable(str)
    End Sub
End Class







0
Tsvetina
Telerik team
answered on 03 Sep 2011, 08:22 AM
Hi Simba,

The AjaxRequest server event of the RadAjaxManager is fired only when calling the ajaxRequest() client method of the control.
Therefore, you should better perform this logic at the end of your ToggleRowSelection method. Try it out this way and let us know in case you encounter any problems.

Greetings,
Tsvetina
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Simba
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Simba
Top achievements
Rank 1
Share this question
or