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

Redisplaying Selected Row in Grid after Postback

4 Answers 211 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ed Navickas
Top achievements
Rank 1
Ed Navickas asked on 25 Nov 2009, 04:07 PM

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  
...  
 

4 Answers, 1 is accepted

Sort by
0
Roland
Top achievements
Rank 1
answered on 26 Nov 2009, 02:05 AM
Hello Ed,

You can try the code in this demo: Selecting rows - server side.
0
Princy
Top achievements
Rank 2
answered on 26 Nov 2009, 06:15 AM
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.
0
Ed Navickas
Top achievements
Rank 1
answered on 01 Dec 2009, 08:27 PM
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 
 
0
Ed Navickas
Top achievements
Rank 1
answered on 02 Dec 2009, 04:23 PM
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
Tags
Grid
Asked by
Ed Navickas
Top achievements
Rank 1
Answers by
Roland
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Ed Navickas
Top achievements
Rank 1
Share this question
or