Redisplaying Selected Row in Grid after Postback

5 posts, 0 answers
  1. Ed Navickas
    Ed Navickas avatar
    5 posts
    Member since:
    Nov 2009

    Posted 25 Nov 2009 Link to this post

    I have a RadGrid on a website where selecting a row triggers a database look-up and a post-back.  When the page refreshes, the row does not appear selected, although the data is retrieved.  We can see the row being selected before the post-back completes.  I've looked at the Grid "Accessing Cells and Rows" demo and I think this may work for my situation, but I have not been able to set the row background from this example.  Anyone have ideas on this?  Any help would be appreciated. More details appear below:

    (ASPX)

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True"   
        AllowPaging="True" DataSourceID="LoggerDS" GridLines="None" > 
        <HeaderContextMenu> 
            <CollapseAnimation Type="OutQuint" Duration="200">  
            </CollapseAnimation> 
        </HeaderContextMenu> 
        <PagerStyle Mode="NextPrevAndNumeric" /> 
        <MasterTableView autogeneratecolumns="False" datasourceid="LoggerDS" CommandItemDisplay="Top" HorizontalAlign="Center">  
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <Columns> 
                <telerik:GridBoundColumn DataField="MDate" DataType="System.DateTime" ItemStyle-HorizontalAlign="Center" 
                    HeaderText="Date" SortExpression="MDate" UniqueName="MDate">  
                    <ItemStyle HorizontalAlign="Center"></ItemStyle> 
                </telerik:GridBoundColumn>        
                <telerik:GridBoundColumn DataField="CallID" DataType="System.Int32" ItemStyle-HorizontalAlign="Center" 
                    HeaderText="Call ID" SortExpression="CallID" UniqueName="CallID" Display="False">  
                    <ItemStyle HorizontalAlign="Center"></ItemStyle> 
                </telerik:GridBoundColumn> 
    ...  
            </Columns> 
            <PagerStyle Mode="NextPrevAndNumeric" /> 
            <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"   
                Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"   
                Wrap="True" /> 
           
                <CommandItemTemplate> 
                    <asp:Button ID="SelectButton" runat="server" Text="Review Selected Call" OnClientClick="return LoadReviewForm();" /> 
                </CommandItemTemplate> 
        </MasterTableView> 
        <FilterMenu> 
            <CollapseAnimation Type="OutQuint" Duration="200">  
            </CollapseAnimation> 
        </FilterMenu> 
        <ClientSettings EnablePostBackOnRowClick="true">  
            <Selecting AllowRowSelect="True"></Selecting> 
        </ClientSettings> 
    </telerik:RadGrid> 
     

    (ASPX.VB)
        Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) Handles RadGrid1.ItemCommand  
     
            If (e.CommandName = "RowClick" AndAlso TypeOf e.Item Is GridDataItem) Then 
                e.Item.Selected = True 
                e.Item.BackColor = Drawing.Color.DarkGray  
    ...  
     
  2. Roland
    Roland avatar
    156 posts
    Member since:
    Feb 2009

    Posted 25 Nov 2009 Link to this post

    Hello Ed,

    You can try the code in this demo: Selecting rows - server side.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 26 Nov 2009 Link to this post

    Hi Ed,

    You can set the background color for the selected item by using the SelectedItemStyle property of the grid:
    aspx:
     <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" AutoGenerateColumns="true" runat="server"
            <SelectedItemStyle CssClass="myselectedclass"/> 

    css:
     .myselectedclass 
       { 
          background:Pink !important; 
       } 

    Thanks
    Princy.
  5. Ed Navickas
    Ed Navickas avatar
    5 posts
    Member since:
    Nov 2009

    Posted 01 Dec 2009 Link to this post

    Thanks for the replies. Unfortunately these recommendations do not work in my case (the selecting rows demo might work but we don't want check boxes).  However, I tracked down the specific part of the code that causes the issue.  We're attaching the grid to a SQLDataSource, and the SelectCommand is built in the code-behind page.  This appears to cause the selection display to fail, although the selected items are still in memory.  When I insert the SelectCommand into the SQLDataSource directly, the page refresh retains the selection display on the grid.  Is there a parameter or some other piece of code I'm missing?  Here is the ASPX and VB for the broken code:
    (ASPX)
        <asp:SqlDataSource ID="LoggerDS" runat="server" SelectCommand=""     
            ConnectionString="<%$ ConnectionStrings:strConnect %>">   
            <SelectParameters> 
                <asp:SessionParameter Type="DateTime" Name="CallTime"   
                    SessionField="StartDate" /> 
                <asp:SessionParameter Name="ID" SessionField="LoggerID"   
                    Type="String" /> 
                <asp:SessionParameter Type="DateTime" Name="CallTime2"   
                    SessionField="EndDate" /> 
            </SelectParameters> 
        </asp:SqlDataSource> 
     
    (VB)
    Protected Sub DDL1_SelectedIndexChanged(...  
     Select Case ...  
      Case 1  
       LoggerDS.SelectCommand = "SELECT ... " 
       Session("GridSelect") = LoggerDS.SelectCommand  
     ...  
    End Sub 
     
    Protected Sub Page_Load(...  
      RadGrid1.DataSourceID = "LoggerDS" 
      LoggerDS.SelectCommand = Session("GridSelect")  
    ...  
    End Sub 
     
  6. Ed Navickas
    Ed Navickas avatar
    5 posts
    Member since:
    Nov 2009

    Posted 02 Dec 2009 Link to this post

    Hi,

    I was able to solve this by adding the test for DataSourceID being Nothing/NULL:

        Protected Sub Page_Load(...  
    ...  
                Select Case ID  
                    Case 1  
                        If RadGrid1.DataSourceID Is Nothing Then 
                            RadGrid1.DataSourceID = "LoggerDS" 
                        End If 
                        LoggerDS.SelectCommand = Session("GridSelect")  
    ...  
     

    For some reason if the DataSourceID is set again it makes the highlight disappear.

    Ed
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017