This is a migrated thread and some comments may be shown as answers.
Select on PreRender not working
5 Answers 104 Views
This is a migrated thread and some comments may be shown as answers.
Tony Upchurch
Top achievements
Rank 1
Tony Upchurch asked on 26 Aug 2013, 08:04 PM
I am trying to select the last row after a postback event but it is not working.  I see the index is valid, but it does not select the row.  I can hard code a number for the index and still nothing is selected.  I fire the postback from Javascript (see code below).  This worked before I upgraded the controls this week.  Any idea why this no longer works with new controls?

function rgBatchMaster_RowClick(sender, eventArgs) {
     sender.get_masterTableView().fireCommand("Select", eventArgs.get_itemIndexHierarchical());
 }

 

protected void rgBatchMaster_PreRender(object sender, EventArgs e)
 {
     rgBatchMaster.Items[4].Selected = true;
     rgBatchMaster.MasterTableView.Items[4].Selected = true;
 }

5 Answers, 1 is accepted

Sort by
0
Paul
Top achievements
Rank 1
answered on 27 Aug 2013, 05:20 PM
I should add some more information about the grid.  I am implementing a click and doubleclick event.  This is why I fire the select from the javascript.  Here is the code for the grid itself.

<telerik:RadGrid runat="server" ID="rgBatchMaster" GridLines="None" AllowPaging="True" PageSize="50" AllowMultiRowSelection="false"
            DataSourceID="dsBatchMaster" AllowSorting="True" SkinID="Default" Skin="Default" CellSpacing="0" Height="700px" Width="100%"
            OnSelectedIndexChanged="rgBatchMaster_SelectedIndexChanged" OnPreRender="rgBatchMaster_PreRender" onkeyup="rgBatchMaster_OnKeyUp" >
            <MasterTableView AutoGenerateColumns="False" DataKeyNames="BatchID" Width="100%"
                DataSourceID="dsBatchMaster" EnableHeaderContextMenu="true" HeaderStyle-Width="100%" >
                <Columns>
                 
                <telerik:GridBoundColumn DataField="BatchID" DefaultInsertValue="" HeaderText="BatchID"
                            ReadOnly="True" SortExpression="BatchID" UniqueName="BatchID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="VendorId" DefaultInsertValue="" HeaderText="VendorID"
                            ReadOnly="True" SortExpression="VendorId" UniqueName="VendorId">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="VendorName" DefaultInsertValue="" HeaderText="Vendor Name" HeaderStyle-Width="250px"
                            ReadOnly="True" SortExpression="VendorName" UniqueName="VendorName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="WarehouseId" DefaultInsertValue="" HeaderText="WH"
                            ReadOnly="True" SortExpression="WarehouseId" UniqueName="WarehouseId">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Description" DefaultInsertValue="" HeaderText="Description"
                            ReadOnly="True" SortExpression="Description" UniqueName="Description">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Type" DefaultInsertValue="" HeaderText="Type"
                            ReadOnly="True" SortExpression="Type" UniqueName="Type">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Cost" DefaultInsertValue="" HeaderText="Cost"
                            ReadOnly="True" SortExpression="Cost" UniqueName="Cost">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Freight" DefaultInsertValue="" HeaderText="Freight"
                            ReadOnly="True" SortExpression="Freight" UniqueName="Freight">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="BatchDate" DefaultInsertValue="" HeaderText="BatchDate"
                            ReadOnly="True" SortExpression="BatchDate" UniqueName="BatchDate" DataFormatString="{0:MM/dd/yyyy}" >
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="DueDate" DefaultInsertValue="" HeaderText="DueDate"
                            ReadOnly="True" SortExpression="DueDate" UniqueName="DueDate" DataFormatString="{0:MM/dd/yyyy}" >
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="PostDate" DefaultInsertValue="" HeaderText="PostDate"
                            ReadOnly="True" SortExpression="PostDate" UniqueName="PostDate" DataFormatString="{0:MM/dd/yyyy}" >
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="InvoiceID" DefaultInsertValue="" HeaderText="InvoiceID"
                            ReadOnly="True" SortExpression="InvoiceID" UniqueName="InvoiceID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="InvoiceDate" DefaultInsertValue="" HeaderText="InvoiceDate"
                            ReadOnly="True" SortExpression="InvoiceDate" UniqueName="InvoiceDate" DataFormatString="{0:MM/dd/yyyy}" >
                </telerik:GridBoundColumn>
 
                </Columns>
            </MasterTableView>
            <ClientSettings AllowColumnsReorder="True" EnablePostBackOnRowClick="false"  >
                    <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                    <Resizing AllowColumnResize="true" AllowResizeToFit="true"  />
                    <Selecting AllowRowSelect="true" EnableDragToSelectRows="true" />
                    <ClientEvents OnRowDblClick="rgBatchMaster_RowDblClick" OnRowClick="rgBatchMaster_RowClick" />
            </ClientSettings>
             
        </telerik:RadGrid>
0
Princy
Top achievements
Rank 1
answered on 28 Aug 2013, 06:12 AM
Hi Paul,

Your requirement is to select a row on row click and double click. Can you please clarify the following doubts I have:

1. You have set AllowRowSelect="true". So the RadGrid will select a row when row is clicked by default. Then why you again need to write code for selecting the row.

2. You wrote code inside prerender event to select 3rd row. so the row will be selected when page loads and post back occurs.

3. I am not sure why you are adding 'sender.get_masterTableView().fireCommand("Select", eventArgs.get_itemIndexHierarchical());' in row click and again code in pre-render event.

Here is the two approach I tried to select a row on single and double click of a row without setting AllowRowSelect to true.

1. Selecting a row from server side by setting EnablePostBackOnRowClick="true" and AllowRowSelect="false".

ASPX:
<telerik:RadGrid runat="server" ID="rgBatchMaster" GridLines="None" AllowPaging="True" PageSize="50" AllowMultiRowSelection="false" DataSourceID="SqlDataSource1"   AllowSorting="True" OnItemCommand="rgBatchMaster_ItemCommand">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID" EnableHeaderContextMenu="true">
        <ClientSettings AllowColumnsReorder="True" EnablePostBackOnRowClick="true">
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
            <Resizing AllowColumnResize="true" AllowResizeToFit="true" />
            <Selecting AllowRowSelect="false" EnableDragToSelectRows="true" />
        </ClientSettings>
        <Columns>
            .  .  .  .
            .  .  .  .
        </Columns>
    </MasterTableView>  
</telerik:RadGrid>

C#:
protected void rgBatchMaster_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    if (e.CommandName == "RowClick")
    {
        GridDataItem item = (GridDataItem)e.Item;
        item.Selected = true;
    }
}

OR

2. Selecting a row from client side by setting EnablePostBackOnRowClick="false" and AllowRowSelect="false".

ASPX:
<telerik:RadGrid runat="server" ID="rgBatchMaster" GridLines="None" AllowPaging="True" PageSize="50" AllowMultiRowSelection="false" DataSourceID="SqlDataSource1"   AllowSorting="True" OnItemCommand="rgBatchMaster_ItemCommand">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID" EnableHeaderContextMenu="true">
        <ClientSettings AllowColumnsReorder="True" EnablePostBackOnRowClick="false">
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
            <Resizing AllowColumnResize="true" AllowResizeToFit="true" />
            <Selecting AllowRowSelect="false" EnableDragToSelectRows="true" />
        </ClientSettings>
        <Columns>
            .  .  .  .
            .  .  .  .
        </Columns>
    </MasterTableView>  
</telerik:RadGrid>

Javascript:
<script type="text/javascript">
    function rgBatchMaster_RowClick(sender, eventArgs) {
        sender.get_masterTableView().fireCommand("Select", eventArgs.get_itemIndexHierarchical());
    }
</script>

 Please elaborate the scenario if it doesn't help.

Thanks,
Princy.
0
Paul
Top achievements
Rank 1
answered on 28 Aug 2013, 12:40 PM
Sorry,  this doesn't work.  I do not understand what you are proposing.  Why would you set the AllowRowSelect to false in both of your solutions?  This is the opposite of what I am trying to accomplish.  My problem is, after postback the last selected row is now longer selected.  Setting it in PreRender does not seem to work.  I am asking why?
0
Paul
Top achievements
Rank 1
answered on 28 Aug 2013, 01:59 PM
If I set AllowRowSelect then the row can not be selected no matter what you do.  Why do you suggest doing this?
0
Princy
Top achievements
Rank 1
answered on 29 Aug 2013, 03:30 AM
Hi Paul,

I apologize for misunderstanding the requirement and misleading you. Please check the following code snippet I tried to persist the row selection.

ASPX:
<telerik:RadGrid runat="server" ID="rgBatchMaster" GridLines="None" AllowPaging="True" PageSize="50" AllowMultiRowSelection="false" DataSourceID="dsBatchMaster"       AllowSorting="True" SkinID="Default" Skin="Default" CellSpacing="0" Height="700px" Width="100%" onitemcommand="rgBatchMaster_ItemCommand"    OnPreRender="rgBatchMaster_PreRender" >
         <MasterTableView AutoGenerateColumns="False" DataKeyNames="BatchID" Width="100%"
                DataSourceID="dsBatchMaster" EnableHeaderContextMenu="true" HeaderStyle-Width="100%" >
                <Columns>               
                <telerik:GridBoundColumn DataField="BatchID" DefaultInsertValue="" HeaderText="BatchID"
                            ReadOnly="True" SortExpression="BatchID" UniqueName="BatchID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="VendorId" DefaultInsertValue="" HeaderText="VendorID"
                            ReadOnly="True" SortExpression="VendorId" UniqueName="VendorId">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="VendorName" DefaultInsertValue="" HeaderText="Vendor Name" HeaderStyle-Width="250px"
                            ReadOnly="True" SortExpression="VendorName" UniqueName="VendorName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="WarehouseId" DefaultInsertValue="" HeaderText="WH"
                            ReadOnly="True" SortExpression="WarehouseId" UniqueName="WarehouseId">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Description" DefaultInsertValue="" HeaderText="Description"
                            ReadOnly="True" SortExpression="Description" UniqueName="Description">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Type" DefaultInsertValue="" HeaderText="Type"
                            ReadOnly="True" SortExpression="Type" UniqueName="Type">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Cost" DefaultInsertValue="" HeaderText="Cost"
                            ReadOnly="True" SortExpression="Cost" UniqueName="Cost">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Freight" DefaultInsertValue="" HeaderText="Freight"
                            ReadOnly="True" SortExpression="Freight" UniqueName="Freight">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="BatchDate" DefaultInsertValue="" HeaderText="BatchDate"
                            ReadOnly="True" SortExpression="BatchDate" UniqueName="BatchDate" DataFormatString="{0:MM/dd/yyyy}" >
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="DueDate" DefaultInsertValue="" HeaderText="DueDate"
                            ReadOnly="True" SortExpression="DueDate" UniqueName="DueDate" DataFormatString="{0:MM/dd/yyyy}" >
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="PostDate" DefaultInsertValue="" HeaderText="PostDate"
                            ReadOnly="True" SortExpression="PostDate" UniqueName="PostDate" DataFormatString="{0:MM/dd/yyyy}" >
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="InvoiceID" DefaultInsertValue="" HeaderText="InvoiceID"
                            ReadOnly="True" SortExpression="InvoiceID" UniqueName="InvoiceID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="InvoiceDate" DefaultInsertValue="" HeaderText="InvoiceDate"
                            ReadOnly="True" SortExpression="InvoiceDate" UniqueName="InvoiceDate" DataFormatString="{0:MM/dd/yyyy}" >
                </telerik:GridBoundColumn>
                </Columns>
        </MasterTableView>
        <ClientSettings AllowColumnsReorder="True" EnablePostBackOnRowClick="false"  >
             <Scrolling AllowScroll="True" UseStaticHeaders="True" />
             <Resizing AllowColumnResize="true" AllowResizeToFit="true"  />
             <Selecting AllowRowSelect="true" EnableDragToSelectRows="true" />
             <ClientEvents OnRowClick="rgBatchMaster_RowClick" />
       </ClientSettings>            
</telerik:RadGrid>

C#:
protected void rgBatchMaster_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    ArrayList selectedItems;
    if (Session["selectedItems"] == null)
    {
        selectedItems = new ArrayList();
    }
    else
    {
        selectedItems = (ArrayList)Session["selectedItems"];
    }
    if (e.CommandName == RadGrid.SelectCommandName && e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        string BatchID = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["BatchID"].ToString();
        selectedItems.Add(BatchID);
        Session["selectedItems"] = selectedItems;
    }
}
protected void rgBatchMaster_PreRender(object sender, EventArgs e)
{
    if (Session["selectedItems"] != null)
    {
        ArrayList selectedItems = (ArrayList)Session["selectedItems"];
        Int16 stackIndex;
        for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
        {
            string curItem = selectedItems[stackIndex].ToString();
            foreach (GridItem item in rgBatchMaster.MasterTableView.Items)
            {
                if (item is GridDataItem)
                {
                    GridDataItem dataItem = (GridDataItem)item;
                    if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["BatchID"].ToString()))
                    {
                        dataItem.Selected = true;
                        break;
                    }
                }
            }
        }
    }
}

Please check this help documentation for more details.

Thanks,
Princy.


Tags
Grid
Asked by
Tony Upchurch
Top achievements
Rank 1
Answers by
Paul
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or